1 /* 2 * Apple Onboard Audio feature call GPIO control 3 * 4 * Copyright 2006 Johannes Berg <johannes@sipsolutions.net> 5 * 6 * GPL v2, can be found in COPYING. 7 * 8 * This file contains the GPIO control routines for 9 * direct (through feature calls) access to the GPIO 10 * registers. 11 */ 12 13 #include <linux/of_irq.h> 14 #include <linux/interrupt.h> 15 #include <asm/pmac_feature.h> 16 #include "../aoa.h" 17 18 /* TODO: these are lots of global variables 19 * that aren't used on most machines... 20 * Move them into a dynamically allocated 21 * structure and use that. 22 */ 23 24 /* these are the GPIO numbers (register addresses as offsets into 25 * the GPIO space) */ 26 static int headphone_mute_gpio; 27 static int master_mute_gpio; 28 static int amp_mute_gpio; 29 static int lineout_mute_gpio; 30 static int hw_reset_gpio; 31 static int lineout_detect_gpio; 32 static int headphone_detect_gpio; 33 static int linein_detect_gpio; 34 35 /* see the SWITCH_GPIO macro */ 36 static int headphone_mute_gpio_activestate; 37 static int master_mute_gpio_activestate; 38 static int amp_mute_gpio_activestate; 39 static int lineout_mute_gpio_activestate; 40 static int hw_reset_gpio_activestate; 41 static int lineout_detect_gpio_activestate; 42 static int headphone_detect_gpio_activestate; 43 static int linein_detect_gpio_activestate; 44 45 /* node pointers that we save when getting the GPIO number 46 * to get the interrupt later */ 47 static struct device_node *lineout_detect_node; 48 static struct device_node *linein_detect_node; 49 static struct device_node *headphone_detect_node; 50 51 static int lineout_detect_irq; 52 static int linein_detect_irq; 53 static int headphone_detect_irq; 54 55 static struct device_node *get_gpio(char *name, 56 char *altname, 57 int *gpioptr, 58 int *gpioactiveptr) 59 { 60 struct device_node *np, *gpio; 61 const u32 *reg; 62 const char *audio_gpio; 63 64 *gpioptr = -1; 65 66 /* check if we can get it the easy way ... */ 67 np = of_find_node_by_name(NULL, name); 68 if (!np) { 69 /* some machines have only gpioX/extint-gpioX nodes, 70 * and an audio-gpio property saying what it is ... 71 * So what we have to do is enumerate all children 72 * of the gpio node and check them all. */ 73 gpio = of_find_node_by_name(NULL, "gpio"); 74 if (!gpio) 75 return NULL; 76 while ((np = of_get_next_child(gpio, np))) { 77 audio_gpio = of_get_property(np, "audio-gpio", NULL); 78 if (!audio_gpio) 79 continue; 80 if (strcmp(audio_gpio, name) == 0) 81 break; 82 if (altname && (strcmp(audio_gpio, altname) == 0)) 83 break; 84 } 85 of_node_put(gpio); 86 /* still not found, assume not there */ 87 if (!np) 88 return NULL; 89 } 90 91 reg = of_get_property(np, "reg", NULL); 92 if (!reg) { 93 of_node_put(np); 94 return NULL; 95 } 96 97 *gpioptr = *reg; 98 99 /* this is a hack, usually the GPIOs 'reg' property 100 * should have the offset based from the GPIO space 101 * which is at 0x50, but apparently not always... */ 102 if (*gpioptr < 0x50) 103 *gpioptr += 0x50; 104 105 reg = of_get_property(np, "audio-gpio-active-state", NULL); 106 if (!reg) 107 /* Apple seems to default to 1, but 108 * that doesn't seem right at least on most 109 * machines. So until proven that the opposite 110 * is necessary, we default to 0 111 * (which, incidentally, snd-powermac also does...) */ 112 *gpioactiveptr = 0; 113 else 114 *gpioactiveptr = *reg; 115 116 return np; 117 } 118 119 static void get_irq(struct device_node * np, int *irqptr) 120 { 121 if (np) 122 *irqptr = irq_of_parse_and_map(np, 0); 123 else 124 *irqptr = 0; 125 } 126 127 /* 0x4 is outenable, 0x1 is out, thus 4 or 5 */ 128 #define SWITCH_GPIO(name, v, on) \ 129 (((v)&~1) | ((on)? \ 130 (name##_gpio_activestate==0?4:5): \ 131 (name##_gpio_activestate==0?5:4))) 132 133 #define FTR_GPIO(name, bit) \ 134 static void ftr_gpio_set_##name(struct gpio_runtime *rt, int on)\ 135 { \ 136 int v; \ 137 \ 138 if (unlikely(!rt)) return; \ 139 \ 140 if (name##_mute_gpio < 0) \ 141 return; \ 142 \ 143 v = pmac_call_feature(PMAC_FTR_READ_GPIO, NULL, \ 144 name##_mute_gpio, \ 145 0); \ 146 \ 147 /* muted = !on... */ \ 148 v = SWITCH_GPIO(name##_mute, v, !on); \ 149 \ 150 pmac_call_feature(PMAC_FTR_WRITE_GPIO, NULL, \ 151 name##_mute_gpio, v); \ 152 \ 153 rt->implementation_private &= ~(1<<bit); \ 154 rt->implementation_private |= (!!on << bit); \ 155 } \ 156 static int ftr_gpio_get_##name(struct gpio_runtime *rt) \ 157 { \ 158 if (unlikely(!rt)) return 0; \ 159 return (rt->implementation_private>>bit)&1; \ 160 } 161 162 FTR_GPIO(headphone, 0); 163 FTR_GPIO(amp, 1); 164 FTR_GPIO(lineout, 2); 165 FTR_GPIO(master, 3); 166 167 static void ftr_gpio_set_hw_reset(struct gpio_runtime *rt, int on) 168 { 169 int v; 170 171 if (unlikely(!rt)) return; 172 if (hw_reset_gpio < 0) 173 return; 174 175 v = pmac_call_feature(PMAC_FTR_READ_GPIO, NULL, 176 hw_reset_gpio, 0); 177 v = SWITCH_GPIO(hw_reset, v, on); 178 pmac_call_feature(PMAC_FTR_WRITE_GPIO, NULL, 179 hw_reset_gpio, v); 180 } 181 182 static struct gpio_methods methods; 183 184 static void ftr_gpio_all_amps_off(struct gpio_runtime *rt) 185 { 186 int saved; 187 188 if (unlikely(!rt)) return; 189 saved = rt->implementation_private; 190 ftr_gpio_set_headphone(rt, 0); 191 ftr_gpio_set_amp(rt, 0); 192 ftr_gpio_set_lineout(rt, 0); 193 if (methods.set_master) 194 ftr_gpio_set_master(rt, 0); 195 rt->implementation_private = saved; 196 } 197 198 static void ftr_gpio_all_amps_restore(struct gpio_runtime *rt) 199 { 200 int s; 201 202 if (unlikely(!rt)) return; 203 s = rt->implementation_private; 204 ftr_gpio_set_headphone(rt, (s>>0)&1); 205 ftr_gpio_set_amp(rt, (s>>1)&1); 206 ftr_gpio_set_lineout(rt, (s>>2)&1); 207 if (methods.set_master) 208 ftr_gpio_set_master(rt, (s>>3)&1); 209 } 210 211 static void ftr_handle_notify(struct work_struct *work) 212 { 213 struct gpio_notification *notif = 214 container_of(work, struct gpio_notification, work.work); 215 216 mutex_lock(¬if->mutex); 217 if (notif->notify) 218 notif->notify(notif->data); 219 mutex_unlock(¬if->mutex); 220 } 221 222 static void gpio_enable_dual_edge(int gpio) 223 { 224 int v; 225 226 if (gpio == -1) 227 return; 228 v = pmac_call_feature(PMAC_FTR_READ_GPIO, NULL, gpio, 0); 229 v |= 0x80; /* enable dual edge */ 230 pmac_call_feature(PMAC_FTR_WRITE_GPIO, NULL, gpio, v); 231 } 232 233 static void ftr_gpio_init(struct gpio_runtime *rt) 234 { 235 get_gpio("headphone-mute", NULL, 236 &headphone_mute_gpio, 237 &headphone_mute_gpio_activestate); 238 get_gpio("amp-mute", NULL, 239 &_mute_gpio, 240 &_mute_gpio_activestate); 241 get_gpio("lineout-mute", NULL, 242 &lineout_mute_gpio, 243 &lineout_mute_gpio_activestate); 244 get_gpio("hw-reset", "audio-hw-reset", 245 &hw_reset_gpio, 246 &hw_reset_gpio_activestate); 247 if (get_gpio("master-mute", NULL, 248 &master_mute_gpio, 249 &master_mute_gpio_activestate)) { 250 methods.set_master = ftr_gpio_set_master; 251 methods.get_master = ftr_gpio_get_master; 252 } 253 254 headphone_detect_node = get_gpio("headphone-detect", NULL, 255 &headphone_detect_gpio, 256 &headphone_detect_gpio_activestate); 257 /* go Apple, and thanks for giving these different names 258 * across the board... */ 259 lineout_detect_node = get_gpio("lineout-detect", "line-output-detect", 260 &lineout_detect_gpio, 261 &lineout_detect_gpio_activestate); 262 linein_detect_node = get_gpio("linein-detect", "line-input-detect", 263 &linein_detect_gpio, 264 &linein_detect_gpio_activestate); 265 266 gpio_enable_dual_edge(headphone_detect_gpio); 267 gpio_enable_dual_edge(lineout_detect_gpio); 268 gpio_enable_dual_edge(linein_detect_gpio); 269 270 get_irq(headphone_detect_node, &headphone_detect_irq); 271 get_irq(lineout_detect_node, &lineout_detect_irq); 272 get_irq(linein_detect_node, &linein_detect_irq); 273 274 ftr_gpio_all_amps_off(rt); 275 rt->implementation_private = 0; 276 INIT_DELAYED_WORK(&rt->headphone_notify.work, ftr_handle_notify); 277 INIT_DELAYED_WORK(&rt->line_in_notify.work, ftr_handle_notify); 278 INIT_DELAYED_WORK(&rt->line_out_notify.work, ftr_handle_notify); 279 mutex_init(&rt->headphone_notify.mutex); 280 mutex_init(&rt->line_in_notify.mutex); 281 mutex_init(&rt->line_out_notify.mutex); 282 } 283 284 static void ftr_gpio_exit(struct gpio_runtime *rt) 285 { 286 ftr_gpio_all_amps_off(rt); 287 rt->implementation_private = 0; 288 if (rt->headphone_notify.notify) 289 free_irq(headphone_detect_irq, &rt->headphone_notify); 290 if (rt->line_in_notify.gpio_private) 291 free_irq(linein_detect_irq, &rt->line_in_notify); 292 if (rt->line_out_notify.gpio_private) 293 free_irq(lineout_detect_irq, &rt->line_out_notify); 294 cancel_delayed_work_sync(&rt->headphone_notify.work); 295 cancel_delayed_work_sync(&rt->line_in_notify.work); 296 cancel_delayed_work_sync(&rt->line_out_notify.work); 297 mutex_destroy(&rt->headphone_notify.mutex); 298 mutex_destroy(&rt->line_in_notify.mutex); 299 mutex_destroy(&rt->line_out_notify.mutex); 300 } 301 302 static irqreturn_t ftr_handle_notify_irq(int xx, void *data) 303 { 304 struct gpio_notification *notif = data; 305 306 schedule_delayed_work(¬if->work, 0); 307 308 return IRQ_HANDLED; 309 } 310 311 static int ftr_set_notify(struct gpio_runtime *rt, 312 enum notify_type type, 313 notify_func_t notify, 314 void *data) 315 { 316 struct gpio_notification *notif; 317 notify_func_t old; 318 int irq; 319 char *name; 320 int err = -EBUSY; 321 322 switch (type) { 323 case AOA_NOTIFY_HEADPHONE: 324 notif = &rt->headphone_notify; 325 name = "headphone-detect"; 326 irq = headphone_detect_irq; 327 break; 328 case AOA_NOTIFY_LINE_IN: 329 notif = &rt->line_in_notify; 330 name = "linein-detect"; 331 irq = linein_detect_irq; 332 break; 333 case AOA_NOTIFY_LINE_OUT: 334 notif = &rt->line_out_notify; 335 name = "lineout-detect"; 336 irq = lineout_detect_irq; 337 break; 338 default: 339 return -EINVAL; 340 } 341 342 if (!irq) 343 return -ENODEV; 344 345 mutex_lock(¬if->mutex); 346 347 old = notif->notify; 348 349 if (!old && !notify) { 350 err = 0; 351 goto out_unlock; 352 } 353 354 if (old && notify) { 355 if (old == notify && notif->data == data) 356 err = 0; 357 goto out_unlock; 358 } 359 360 if (old && !notify) 361 free_irq(irq, notif); 362 363 if (!old && notify) { 364 err = request_irq(irq, ftr_handle_notify_irq, 0, name, notif); 365 if (err) 366 goto out_unlock; 367 } 368 369 notif->notify = notify; 370 notif->data = data; 371 372 err = 0; 373 out_unlock: 374 mutex_unlock(¬if->mutex); 375 return err; 376 } 377 378 static int ftr_get_detect(struct gpio_runtime *rt, 379 enum notify_type type) 380 { 381 int gpio, ret, active; 382 383 switch (type) { 384 case AOA_NOTIFY_HEADPHONE: 385 gpio = headphone_detect_gpio; 386 active = headphone_detect_gpio_activestate; 387 break; 388 case AOA_NOTIFY_LINE_IN: 389 gpio = linein_detect_gpio; 390 active = linein_detect_gpio_activestate; 391 break; 392 case AOA_NOTIFY_LINE_OUT: 393 gpio = lineout_detect_gpio; 394 active = lineout_detect_gpio_activestate; 395 break; 396 default: 397 return -EINVAL; 398 } 399 400 if (gpio == -1) 401 return -ENODEV; 402 403 ret = pmac_call_feature(PMAC_FTR_READ_GPIO, NULL, gpio, 0); 404 if (ret < 0) 405 return ret; 406 return ((ret >> 1) & 1) == active; 407 } 408 409 static struct gpio_methods methods = { 410 .init = ftr_gpio_init, 411 .exit = ftr_gpio_exit, 412 .all_amps_off = ftr_gpio_all_amps_off, 413 .all_amps_restore = ftr_gpio_all_amps_restore, 414 .set_headphone = ftr_gpio_set_headphone, 415 .set_speakers = ftr_gpio_set_amp, 416 .set_lineout = ftr_gpio_set_lineout, 417 .set_hw_reset = ftr_gpio_set_hw_reset, 418 .get_headphone = ftr_gpio_get_headphone, 419 .get_speakers = ftr_gpio_get_amp, 420 .get_lineout = ftr_gpio_get_lineout, 421 .set_notify = ftr_set_notify, 422 .get_detect = ftr_get_detect, 423 }; 424 425 struct gpio_methods *ftr_gpio_methods = &methods; 426 EXPORT_SYMBOL_GPL(ftr_gpio_methods); 427