blob: c6be11985591940cdaea38d4d5826dcfc26ae1e6 [file] [log] [blame]
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001/* Copyright (c) 2010-2011, Code Aurora Forum. All rights reserved.
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 */
12
13#define pr_fmt(fmt) "%s: " fmt, __func__
14
15#include <linux/module.h>
16#include <linux/init.h>
17#include <linux/kernel.h>
18#include <linux/interrupt.h>
19#include <linux/input.h>
20#include <linux/platform_device.h>
21#include <linux/gpio.h>
22#include <linux/switch.h>
23#include <linux/pm.h>
24#include <linux/slab.h>
25#include <linux/pm_runtime.h>
26#include <linux/hrtimer.h>
27#include <linux/delay.h>
28#include <linux/regulator/consumer.h>
29
30#include <linux/mfd/pmic8058.h>
31#include <linux/pmic8058-othc.h>
32#include <linux/msm_adc.h>
33
34#define PM8058_OTHC_LOW_CURR_MASK 0xF0
35#define PM8058_OTHC_HIGH_CURR_MASK 0x0F
36#define PM8058_OTHC_EN_SIG_MASK 0x3F
37#define PM8058_OTHC_HYST_PREDIV_MASK 0xC7
38#define PM8058_OTHC_CLK_PREDIV_MASK 0xF8
39#define PM8058_OTHC_HYST_CLK_MASK 0x0F
40#define PM8058_OTHC_PERIOD_CLK_MASK 0xF0
41
42#define PM8058_OTHC_LOW_CURR_SHIFT 0x4
43#define PM8058_OTHC_EN_SIG_SHIFT 0x6
44#define PM8058_OTHC_HYST_PREDIV_SHIFT 0x3
45#define PM8058_OTHC_HYST_CLK_SHIFT 0x4
46
47#define OTHC_GPIO_MAX_LEN 25
48
49struct pm8058_othc {
50 bool othc_sw_state;
51 bool switch_reject;
52 bool othc_support_n_switch;
53 bool accessory_support;
54 bool accessories_adc_support;
55 int othc_base;
56 int othc_irq_sw;
57 int othc_irq_ir;
58 int othc_ir_state;
59 int num_accessories;
60 int curr_accessory_code;
61 int curr_accessory;
62 int video_out_gpio;
63 u32 sw_key_code;
64 u32 accessories_adc_channel;
65 int ir_gpio;
66 unsigned long switch_debounce_ms;
67 unsigned long detection_delay_ms;
68 void *adc_handle;
69 void *accessory_adc_handle;
70 spinlock_t lock;
71 struct regulator *othc_vreg;
72 struct input_dev *othc_ipd;
73 struct switch_dev othc_sdev;
74 struct pmic8058_othc_config_pdata *othc_pdata;
75 struct othc_accessory_info *accessory_info;
76 struct hrtimer timer;
77 struct othc_n_switch_config *switch_config;
78 struct pm8058_chip *pm_chip;
79 struct work_struct switch_work;
80 struct delayed_work detect_work;
81 struct delayed_work hs_work;
82};
83
84static struct pm8058_othc *config[OTHC_MICBIAS_MAX];
85
86static void hs_worker(struct work_struct *work)
87{
88 int rc;
89 struct pm8058_othc *dd =
90 container_of(work, struct pm8058_othc, hs_work.work);
91
92 rc = gpio_get_value_cansleep(dd->ir_gpio);
93 if (rc < 0) {
94 pr_err("Unable to read IR GPIO\n");
95 enable_irq(dd->othc_irq_ir);
96 return;
97 }
98
99 dd->othc_ir_state = !rc;
100 schedule_delayed_work(&dd->detect_work,
101 msecs_to_jiffies(dd->detection_delay_ms));
102}
103
104static irqreturn_t ir_gpio_irq(int irq, void *dev_id)
105{
106 unsigned long flags;
107 struct pm8058_othc *dd = dev_id;
108
109 spin_lock_irqsave(&dd->lock, flags);
110 /* Enable the switch reject flag */
111 dd->switch_reject = true;
112 spin_unlock_irqrestore(&dd->lock, flags);
113
114 /* Start the HR timer if one is not active */
115 if (hrtimer_active(&dd->timer))
116 hrtimer_cancel(&dd->timer);
117
118 hrtimer_start(&dd->timer,
119 ktime_set((dd->switch_debounce_ms / 1000),
120 (dd->switch_debounce_ms % 1000) * 1000000), HRTIMER_MODE_REL);
121
122 /* disable irq, this gets enabled in the workqueue */
123 disable_irq_nosync(dd->othc_irq_ir);
124 schedule_delayed_work(&dd->hs_work, 0);
125
126 return IRQ_HANDLED;
127}
128/*
129 * The API pm8058_micbias_enable() allows to configure
130 * the MIC_BIAS. Only the lines which are not used for
131 * headset detection can be configured using this API.
132 * The API returns an error code if it fails to configure
133 * the specified MIC_BIAS line, else it returns 0.
134 */
135int pm8058_micbias_enable(enum othc_micbias micbias,
136 enum othc_micbias_enable enable)
137{
138 int rc;
139 u8 reg;
140 struct pm8058_othc *dd = config[micbias];
141
142 if (dd == NULL) {
143 pr_err("MIC_BIAS not registered, cannot enable\n");
144 return -ENODEV;
145 }
146
147 if (dd->othc_pdata->micbias_capability != OTHC_MICBIAS) {
148 pr_err("MIC_BIAS enable capability not supported\n");
149 return -EINVAL;
150 }
151
152 rc = pm8058_read(dd->pm_chip, dd->othc_base + 1, &reg, 1);
153 if (rc < 0) {
154 pr_err("PM8058 read failed\n");
155 return rc;
156 }
157
158 reg &= PM8058_OTHC_EN_SIG_MASK;
159 reg |= (enable << PM8058_OTHC_EN_SIG_SHIFT);
160
161 rc = pm8058_write(dd->pm_chip, dd->othc_base + 1, &reg, 1);
162 if (rc < 0) {
163 pr_err("PM8058 write failed\n");
164 return rc;
165 }
166
167 return rc;
168}
169EXPORT_SYMBOL(pm8058_micbias_enable);
170
171int pm8058_othc_svideo_enable(enum othc_micbias micbias, bool enable)
172{
173 struct pm8058_othc *dd = config[micbias];
174
175 if (dd == NULL) {
176 pr_err("MIC_BIAS not registered, cannot enable\n");
177 return -ENODEV;
178 }
179
180 if (dd->othc_pdata->micbias_capability != OTHC_MICBIAS_HSED) {
181 pr_err("MIC_BIAS enable capability not supported\n");
182 return -EINVAL;
183 }
184
185 if (dd->accessories_adc_support) {
186 /* GPIO state for MIC_IN = 0, SVIDEO = 1 */
187 gpio_set_value_cansleep(dd->video_out_gpio, !!enable);
188 if (enable) {
189 pr_debug("Enable the video path\n");
190 switch_set_state(&dd->othc_sdev, dd->curr_accessory);
191 input_report_switch(dd->othc_ipd,
192 dd->curr_accessory_code, 1);
193 input_sync(dd->othc_ipd);
194 } else {
195 pr_debug("Disable the video path\n");
196 switch_set_state(&dd->othc_sdev, 0);
197 input_report_switch(dd->othc_ipd,
198 dd->curr_accessory_code, 0);
199 input_sync(dd->othc_ipd);
200 }
201 }
202
203 return 0;
204}
205EXPORT_SYMBOL(pm8058_othc_svideo_enable);
206
207#ifdef CONFIG_PM
208static int pm8058_othc_suspend(struct device *dev)
209{
210 int rc = 0;
211 struct pm8058_othc *dd = dev_get_drvdata(dev);
212
213 if (dd->othc_pdata->micbias_capability == OTHC_MICBIAS_HSED) {
214 if (device_may_wakeup(dev)) {
215 enable_irq_wake(dd->othc_irq_sw);
216 enable_irq_wake(dd->othc_irq_ir);
217 }
218 }
219
220 if (!device_may_wakeup(dev)) {
221 rc = regulator_disable(dd->othc_vreg);
222 if (rc)
223 pr_err("othc micbais power off failed\n");
224 }
225
226 return rc;
227}
228
229static int pm8058_othc_resume(struct device *dev)
230{
231 int rc = 0;
232 struct pm8058_othc *dd = dev_get_drvdata(dev);
233
234 if (dd->othc_pdata->micbias_capability == OTHC_MICBIAS_HSED) {
235 if (device_may_wakeup(dev)) {
236 disable_irq_wake(dd->othc_irq_sw);
237 disable_irq_wake(dd->othc_irq_ir);
238 }
239 }
240
241 if (!device_may_wakeup(dev)) {
242 rc = regulator_enable(dd->othc_vreg);
243 if (rc)
244 pr_err("othc micbais power on failed\n");
245 }
246
247 return rc;
248}
249
250static struct dev_pm_ops pm8058_othc_pm_ops = {
251 .suspend = pm8058_othc_suspend,
252 .resume = pm8058_othc_resume,
253};
254#endif
255
256static int __devexit pm8058_othc_remove(struct platform_device *pd)
257{
258 struct pm8058_othc *dd = platform_get_drvdata(pd);
259
260 pm_runtime_set_suspended(&pd->dev);
261 pm_runtime_disable(&pd->dev);
262
263 if (dd->othc_pdata->micbias_capability == OTHC_MICBIAS_HSED) {
264 device_init_wakeup(&pd->dev, 0);
265 if (dd->othc_support_n_switch == true) {
266 adc_channel_close(dd->adc_handle);
267 cancel_work_sync(&dd->switch_work);
268 }
269
270 if (dd->accessory_support == true) {
271 int i;
272 for (i = 0; i < dd->num_accessories; i++) {
273 if (dd->accessory_info[i].detect_flags &
274 OTHC_GPIO_DETECT)
275 gpio_free(dd->accessory_info[i].gpio);
276 }
277 }
278 cancel_delayed_work_sync(&dd->detect_work);
279 cancel_delayed_work_sync(&dd->hs_work);
280 free_irq(dd->othc_irq_sw, dd);
281 free_irq(dd->othc_irq_ir, dd);
282 if (dd->ir_gpio != -1)
283 gpio_free(dd->ir_gpio);
284 input_unregister_device(dd->othc_ipd);
285 }
286 regulator_disable(dd->othc_vreg);
287 regulator_put(dd->othc_vreg);
288
289 kfree(dd);
290
291 return 0;
292}
293
294static enum hrtimer_restart pm8058_othc_timer(struct hrtimer *timer)
295{
296 unsigned long flags;
297 struct pm8058_othc *dd = container_of(timer,
298 struct pm8058_othc, timer);
299
300 spin_lock_irqsave(&dd->lock, flags);
301 dd->switch_reject = false;
302 spin_unlock_irqrestore(&dd->lock, flags);
303
304 return HRTIMER_NORESTART;
305}
306
307static void othc_report_switch(struct pm8058_othc *dd, u32 res)
308{
309 u8 i;
310 struct othc_switch_info *sw_info = dd->switch_config->switch_info;
311
312 for (i = 0; i < dd->switch_config->num_keys; i++) {
313 if (res >= sw_info[i].min_adc_threshold &&
314 res <= sw_info[i].max_adc_threshold) {
315 dd->othc_sw_state = true;
316 dd->sw_key_code = sw_info[i].key_code;
317 input_report_key(dd->othc_ipd, sw_info[i].key_code, 1);
318 input_sync(dd->othc_ipd);
319 return;
320 }
321 }
322
323 /*
324 * If the switch is not present in a specified ADC range
325 * report a default switch press.
326 */
327 if (dd->switch_config->default_sw_en) {
328 dd->othc_sw_state = true;
329 dd->sw_key_code =
330 sw_info[dd->switch_config->default_sw_idx].key_code;
331 input_report_key(dd->othc_ipd, dd->sw_key_code, 1);
332 input_sync(dd->othc_ipd);
333 }
334}
335
336static void switch_work_f(struct work_struct *work)
337{
338 int rc, i;
339 u32 res = 0;
340 struct adc_chan_result adc_result;
341 struct pm8058_othc *dd =
342 container_of(work, struct pm8058_othc, switch_work);
343 DECLARE_COMPLETION_ONSTACK(adc_wait);
344 u8 num_adc_samples = dd->switch_config->num_adc_samples;
345
346 /* sleep for settling time */
347 msleep(dd->switch_config->voltage_settling_time_ms);
348
349 for (i = 0; i < num_adc_samples; i++) {
350 rc = adc_channel_request_conv(dd->adc_handle, &adc_wait);
351 if (rc) {
352 pr_err("adc_channel_request_conv failed\n");
353 goto bail_out;
354 }
355 rc = wait_for_completion_interruptible(&adc_wait);
356 if (rc) {
357 pr_err("wait_for_completion_interruptible failed\n");
358 goto bail_out;
359 }
360 rc = adc_channel_read_result(dd->adc_handle, &adc_result);
361 if (rc) {
362 pr_err("adc_channel_read_result failed\n");
363 goto bail_out;
364 }
365 res += adc_result.physical;
366 }
367bail_out:
368 if (i == num_adc_samples && num_adc_samples != 0) {
369 res /= num_adc_samples;
370 othc_report_switch(dd, res);
371 } else
372 pr_err("Insufficient ADC samples\n");
373
374 enable_irq(dd->othc_irq_sw);
375}
376
377static int accessory_adc_detect(struct pm8058_othc *dd, int accessory)
378{
379 int rc;
380 u32 res;
381 struct adc_chan_result accessory_adc_result;
382 DECLARE_COMPLETION_ONSTACK(accessory_adc_wait);
383
384 rc = adc_channel_request_conv(dd->accessory_adc_handle,
385 &accessory_adc_wait);
386 if (rc) {
387 pr_err("adc_channel_request_conv failed\n");
388 goto adc_failed;
389 }
390 rc = wait_for_completion_interruptible(&accessory_adc_wait);
391 if (rc) {
392 pr_err("wait_for_completion_interruptible failed\n");
393 goto adc_failed;
394 }
395 rc = adc_channel_read_result(dd->accessory_adc_handle,
396 &accessory_adc_result);
397 if (rc) {
398 pr_err("adc_channel_read_result failed\n");
399 goto adc_failed;
400 }
401
402 res = accessory_adc_result.physical;
403
404 if (res >= dd->accessory_info[accessory].adc_thres.min_threshold &&
405 res <= dd->accessory_info[accessory].adc_thres.max_threshold) {
406 pr_debug("Accessory on ADC detected!, ADC Value = %u\n", res);
407 return 1;
408 }
409
410adc_failed:
411 return 0;
412}
413
414
415static int pm8058_accessory_report(struct pm8058_othc *dd, int status)
416{
417 int i, rc, detected = 0;
418 u8 micbias_status, switch_status;
419
420 if (dd->accessory_support == false) {
421 /* Report default headset */
422 switch_set_state(&dd->othc_sdev, !!status);
423 input_report_switch(dd->othc_ipd, SW_HEADPHONE_INSERT,
424 !!status);
425 input_sync(dd->othc_ipd);
426 return 0;
427 }
428
429 /* For accessory */
430 if (dd->accessory_support == true && status == 0) {
431 /* Report removal of the accessory. */
432
433 /*
434 * If the current accessory is video cable, reject the removal
435 * interrupt.
436 */
437 pr_info("Accessory [%d] removed\n", dd->curr_accessory);
438 if (dd->curr_accessory == OTHC_SVIDEO_OUT)
439 return 0;
440
441 switch_set_state(&dd->othc_sdev, 0);
442 input_report_switch(dd->othc_ipd, dd->curr_accessory_code, 0);
443 input_sync(dd->othc_ipd);
444 return 0;
445 }
446
447 if (dd->ir_gpio < 0) {
448 /* Check the MIC_BIAS status */
449 rc = pm8058_irq_get_rt_status(dd->pm_chip, dd->othc_irq_ir);
450 if (rc < 0) {
451 pr_err("Unable to read IR status from PMIC\n");
452 goto fail_ir_accessory;
453 }
454 micbias_status = !!rc;
455 } else {
456 rc = gpio_get_value_cansleep(dd->ir_gpio);
457 if (rc < 0) {
458 pr_err("Unable to read IR status from GPIO\n");
459 goto fail_ir_accessory;
460 }
461 micbias_status = !rc;
462 }
463
464 /* Check the switch status */
465 rc = pm8058_irq_get_rt_status(dd->pm_chip, dd->othc_irq_sw);
466 if (rc < 0) {
467 pr_err("Unable to read SWITCH status\n");
468 goto fail_ir_accessory;
469 }
470 switch_status = !!rc;
471
472 /* Loop through to check which accessory is connected */
473 for (i = 0; i < dd->num_accessories; i++) {
474 detected = 0;
475 if (dd->accessory_info[i].enabled == false)
476 continue;
477
478 if (dd->accessory_info[i].detect_flags & OTHC_MICBIAS_DETECT) {
479 if (micbias_status)
480 detected = 1;
481 else
482 continue;
483 }
484 if (dd->accessory_info[i].detect_flags & OTHC_SWITCH_DETECT) {
485 if (switch_status)
486 detected = 1;
487 else
488 continue;
489 }
490 if (dd->accessory_info[i].detect_flags & OTHC_GPIO_DETECT) {
491 rc = gpio_get_value_cansleep(
492 dd->accessory_info[i].gpio);
493 if (rc < 0)
494 continue;
495
496 if (rc ^ dd->accessory_info[i].active_low)
497 detected = 1;
498 else
499 continue;
500 }
501 if (dd->accessory_info[i].detect_flags & OTHC_ADC_DETECT)
502 detected = accessory_adc_detect(dd, i);
503
504 if (detected)
505 break;
506 }
507
508 if (detected) {
509 dd->curr_accessory = dd->accessory_info[i].accessory;
510 dd->curr_accessory_code = dd->accessory_info[i].key_code;
511
512 /* if Video out cable detected enable the video path*/
513 if (dd->curr_accessory == OTHC_SVIDEO_OUT) {
514 pm8058_othc_svideo_enable(
515 dd->othc_pdata->micbias_select, true);
516
517 } else {
518 switch_set_state(&dd->othc_sdev, dd->curr_accessory);
519 input_report_switch(dd->othc_ipd,
520 dd->curr_accessory_code, 1);
521 input_sync(dd->othc_ipd);
522 }
523 pr_info("Accessory [%d] inserted\n", dd->curr_accessory);
524 } else
525 pr_info("Unable to detect accessory. False interrupt!\n");
526
527 return 0;
528
529fail_ir_accessory:
530 return rc;
531}
532
533static void detect_work_f(struct work_struct *work)
534{
535 int rc;
536 struct pm8058_othc *dd =
537 container_of(work, struct pm8058_othc, detect_work.work);
538
539 if (dd->othc_ir_state) {
540 /* inserted */
541 rc = pm8058_accessory_report(dd, 1);
542 if (rc)
543 pr_err("Accessory could not be detected\n");
544 } else {
545 /* removed */
546 rc = pm8058_accessory_report(dd, 0);
547 if (rc)
548 pr_err("Accessory could not be detected\n");
549 /* Clear existing switch state */
550 dd->othc_sw_state = false;
551 }
552 enable_irq(dd->othc_irq_ir);
553}
554
555/*
556 * The pm8058_no_sw detects the switch press and release operation.
557 * The odd number call is press and even number call is release.
558 * The current state of the button is maintained in othc_sw_state variable.
559 * This isr gets called only for NO type headsets.
560 */
561static irqreturn_t pm8058_no_sw(int irq, void *dev_id)
562{
563 int level;
564 struct pm8058_othc *dd = dev_id;
565 unsigned long flags;
566
567 /* Check if headset has been inserted, else return */
568 if (!dd->othc_ir_state)
569 return IRQ_HANDLED;
570
571 spin_lock_irqsave(&dd->lock, flags);
572 if (dd->switch_reject == true) {
573 pr_debug("Rejected switch interrupt\n");
574 spin_unlock_irqrestore(&dd->lock, flags);
575 return IRQ_HANDLED;
576 }
577 spin_unlock_irqrestore(&dd->lock, flags);
578
579 level = pm8058_irq_get_rt_status(dd->pm_chip, dd->othc_irq_sw);
580 if (level < 0) {
581 pr_err("Unable to read IRQ status register\n");
582 return IRQ_HANDLED;
583 }
584
585 if (dd->othc_support_n_switch == true) {
586 if (level == 0) {
587 dd->othc_sw_state = false;
588 input_report_key(dd->othc_ipd, dd->sw_key_code, 0);
589 input_sync(dd->othc_ipd);
590 } else {
591 disable_irq_nosync(dd->othc_irq_sw);
592 schedule_work(&dd->switch_work);
593 }
594 return IRQ_HANDLED;
595 }
596 /*
597 * It is necessary to check the software state and the hardware state
598 * to make sure that the residual interrupt after the debounce time does
599 * not disturb the software state machine.
600 */
601 if (level == 1 && dd->othc_sw_state == false) {
602 /* Switch has been pressed */
603 dd->othc_sw_state = true;
604 input_report_key(dd->othc_ipd, KEY_MEDIA, 1);
605 } else if (level == 0 && dd->othc_sw_state == true) {
606 /* Switch has been released */
607 dd->othc_sw_state = false;
608 input_report_key(dd->othc_ipd, KEY_MEDIA, 0);
609 }
610 input_sync(dd->othc_ipd);
611
612 return IRQ_HANDLED;
613}
614
615/*
616 * The pm8058_nc_ir detects insert / remove of the headset (for NO),
617 * The current state of the headset is maintained in othc_ir_state variable.
618 * Due to a hardware bug, false switch interrupts are seen during headset
619 * insert. This is handled in the software by rejecting the switch interrupts
620 * for a small period of time after the headset has been inserted.
621 */
622static irqreturn_t pm8058_nc_ir(int irq, void *dev_id)
623{
624 unsigned long flags, rc;
625 struct pm8058_othc *dd = dev_id;
626
627 spin_lock_irqsave(&dd->lock, flags);
628 /* Enable the switch reject flag */
629 dd->switch_reject = true;
630 spin_unlock_irqrestore(&dd->lock, flags);
631
632 /* Start the HR timer if one is not active */
633 if (hrtimer_active(&dd->timer))
634 hrtimer_cancel(&dd->timer);
635
636 hrtimer_start(&dd->timer,
637 ktime_set((dd->switch_debounce_ms / 1000),
638 (dd->switch_debounce_ms % 1000) * 1000000), HRTIMER_MODE_REL);
639
640 /* disable irq, this gets enabled in the workqueue */
641 disable_irq_nosync(dd->othc_irq_ir);
642
643 /* Check the MIC_BIAS status, to check if inserted or removed */
644 rc = pm8058_irq_get_rt_status(dd->pm_chip, dd->othc_irq_ir);
645 if (rc < 0) {
646 pr_err("Unable to read IR status\n");
647 goto fail_ir;
648 }
649
650 dd->othc_ir_state = rc;
651 schedule_delayed_work(&dd->detect_work,
652 msecs_to_jiffies(dd->detection_delay_ms));
653
654fail_ir:
655 return IRQ_HANDLED;
656}
657
658static int pm8058_configure_micbias(struct pm8058_othc *dd)
659{
660 int rc;
661 u8 reg, value;
662 u32 value1;
663 u16 base_addr = dd->othc_base;
664 struct hsed_bias_config *hsed_config =
665 dd->othc_pdata->hsed_config->hsed_bias_config;
666
667 /* Intialize the OTHC module */
668 /* Control Register 1*/
669 rc = pm8058_read(dd->pm_chip, base_addr, &reg, 1);
670 if (rc < 0) {
671 pr_err("PM8058 read failed\n");
672 return rc;
673 }
674
675 /* set iDAC high current threshold */
676 value = (hsed_config->othc_highcurr_thresh_uA / 100) - 2;
677 reg = (reg & PM8058_OTHC_HIGH_CURR_MASK) | value;
678
679 rc = pm8058_write(dd->pm_chip, base_addr, &reg, 1);
680 if (rc < 0) {
681 pr_err("PM8058 read failed\n");
682 return rc;
683 }
684
685 /* Control register 2*/
686 rc = pm8058_read(dd->pm_chip, base_addr + 1, &reg, 1);
687 if (rc < 0) {
688 pr_err("PM8058 read failed\n");
689 return rc;
690 }
691
692 value = dd->othc_pdata->micbias_enable;
693 reg &= PM8058_OTHC_EN_SIG_MASK;
694 reg |= (value << PM8058_OTHC_EN_SIG_SHIFT);
695
696 value = 0;
697 value1 = (hsed_config->othc_hyst_prediv_us << 10) / USEC_PER_SEC;
698 while (value1 != 0) {
699 value1 = value1 >> 1;
700 value++;
701 }
702 if (value > 7) {
703 pr_err("Invalid input argument - othc_hyst_prediv_us\n");
704 return -EINVAL;
705 }
706 reg &= PM8058_OTHC_HYST_PREDIV_MASK;
707 reg |= (value << PM8058_OTHC_HYST_PREDIV_SHIFT);
708
709 value = 0;
710 value1 = (hsed_config->othc_period_clkdiv_us << 10) / USEC_PER_SEC;
711 while (value1 != 1) {
712 value1 = value1 >> 1;
713 value++;
714 }
715 if (value > 8) {
716 pr_err("Invalid input argument - othc_period_clkdiv_us\n");
717 return -EINVAL;
718 }
719 reg = (reg & PM8058_OTHC_CLK_PREDIV_MASK) | (value - 1);
720
721 rc = pm8058_write(dd->pm_chip, base_addr + 1, &reg, 1);
722 if (rc < 0) {
723 pr_err("PM8058 read failed\n");
724 return rc;
725 }
726
727 /* Control register 3 */
728 rc = pm8058_read(dd->pm_chip, base_addr + 2 , &reg, 1);
729 if (rc < 0) {
730 pr_err("PM8058 read failed\n");
731 return rc;
732 }
733
734 value = hsed_config->othc_hyst_clk_us /
735 hsed_config->othc_hyst_prediv_us;
736 if (value > 15) {
737 pr_err("Invalid input argument - othc_hyst_prediv_us\n");
738 return -EINVAL;
739 }
740 reg &= PM8058_OTHC_HYST_CLK_MASK;
741 reg |= value << PM8058_OTHC_HYST_CLK_SHIFT;
742
743 value = hsed_config->othc_period_clk_us /
744 hsed_config->othc_period_clkdiv_us;
745 if (value > 15) {
746 pr_err("Invalid input argument - othc_hyst_prediv_us\n");
747 return -EINVAL;
748 }
749 reg = (reg & PM8058_OTHC_PERIOD_CLK_MASK) | value;
750
751 rc = pm8058_write(dd->pm_chip, base_addr + 2, &reg, 1);
752 if (rc < 0) {
753 pr_err("PM8058 read failed\n");
754 return rc;
755 }
756
757 return 0;
758}
759
760static ssize_t othc_headset_print_name(struct switch_dev *sdev, char *buf)
761{
762 switch (switch_get_state(sdev)) {
763 case OTHC_NO_DEVICE:
764 return sprintf(buf, "No Device\n");
765 case OTHC_HEADSET:
766 case OTHC_HEADPHONE:
767 case OTHC_MICROPHONE:
768 case OTHC_ANC_HEADSET:
769 case OTHC_ANC_HEADPHONE:
770 case OTHC_ANC_MICROPHONE:
771 return sprintf(buf, "Headset\n");
772 }
773 return -EINVAL;
774}
775
776static int pm8058_configure_switch(struct pm8058_othc *dd)
777{
778 int rc, i;
779
780 if (dd->othc_support_n_switch == true) {
781 /* n-switch support */
782 rc = adc_channel_open(dd->switch_config->adc_channel,
783 &dd->adc_handle);
784 if (rc) {
785 pr_err("Unable to open ADC channel\n");
786 return -ENODEV;
787 }
788
789 for (i = 0; i < dd->switch_config->num_keys; i++) {
790 input_set_capability(dd->othc_ipd, EV_KEY,
791 dd->switch_config->switch_info[i].key_code);
792 }
793 } else /* Only single switch supported */
794 input_set_capability(dd->othc_ipd, EV_KEY, KEY_MEDIA);
795
796 return 0;
797}
798
799static int
800pm8058_configure_accessory(struct pm8058_othc *dd)
801{
802 int i, rc;
803 char name[OTHC_GPIO_MAX_LEN];
804
805 /*
806 * Not bailing out if the gpio_* configure calls fail. This is required
807 * as multiple accessories are detected by the same gpio.
808 */
809 for (i = 0; i < dd->num_accessories; i++) {
810 if (dd->accessory_info[i].enabled == false)
811 continue;
812 if (dd->accessory_info[i].detect_flags & OTHC_GPIO_DETECT) {
813 snprintf(name, OTHC_GPIO_MAX_LEN, "%s%d",
814 "othc_acc_gpio_", i);
815 rc = gpio_request(dd->accessory_info[i].gpio, name);
816 if (rc) {
817 pr_debug("Unable to request GPIO [%d]\n",
818 dd->accessory_info[i].gpio);
819 continue;
820 }
821 rc = gpio_direction_input(dd->accessory_info[i].gpio);
822 if (rc) {
823 pr_debug("Unable to set-direction GPIO [%d]\n",
824 dd->accessory_info[i].gpio);
825 gpio_free(dd->accessory_info[i].gpio);
826 continue;
827 }
828 }
829 input_set_capability(dd->othc_ipd, EV_SW,
830 dd->accessory_info[i].key_code);
831 }
832
833 if (dd->accessories_adc_support) {
834 /*
835 * Check if 3 switch is supported. If both are using the same
836 * ADC channel, the same handle can be used.
837 */
838 if (dd->othc_support_n_switch) {
839 if (dd->adc_handle != NULL &&
840 (dd->accessories_adc_channel ==
841 dd->switch_config->adc_channel))
842 dd->accessory_adc_handle = dd->adc_handle;
843 } else {
844 rc = adc_channel_open(dd->accessories_adc_channel,
845 &dd->accessory_adc_handle);
846 if (rc) {
847 pr_err("Unable to open ADC channel\n");
848 rc = -ENODEV;
849 goto accessory_adc_fail;
850 }
851 }
852 if (dd->video_out_gpio != 0) {
853 rc = gpio_request(dd->video_out_gpio, "vout_enable");
854 if (rc < 0) {
855 pr_err("request VOUT gpio failed (%d)\n", rc);
856 goto accessory_adc_fail;
857 }
858 rc = gpio_direction_output(dd->video_out_gpio, 0);
859 if (rc < 0) {
860 pr_err("direction_out failed (%d)\n", rc);
861 goto accessory_adc_fail;
862 }
863 }
864
865 }
866
867 return 0;
868
869accessory_adc_fail:
870 for (i = 0; i < dd->num_accessories; i++) {
871 if (dd->accessory_info[i].enabled == false)
872 continue;
873 gpio_free(dd->accessory_info[i].gpio);
874 }
875 return rc;
876}
877
878static int
879othc_configure_hsed(struct pm8058_othc *dd, struct platform_device *pd)
880{
881 int rc;
882 struct input_dev *ipd;
883 struct pmic8058_othc_config_pdata *pdata = pd->dev.platform_data;
884 struct othc_hsed_config *hsed_config = pdata->hsed_config;
885
886 dd->othc_sdev.name = "h2w";
887 dd->othc_sdev.print_name = othc_headset_print_name;
888
889 rc = switch_dev_register(&dd->othc_sdev);
890 if (rc) {
891 pr_err("Unable to register switch device\n");
892 return rc;
893 }
894
895 ipd = input_allocate_device();
896 if (ipd == NULL) {
897 pr_err("Unable to allocate memory\n");
898 rc = -ENOMEM;
899 goto fail_input_alloc;
900 }
901
902 /* Get the IRQ for Headset Insert-remove and Switch-press */
903 dd->othc_irq_sw = platform_get_irq(pd, 0);
904 dd->othc_irq_ir = platform_get_irq(pd, 1);
905 if (dd->othc_irq_ir < 0 || dd->othc_irq_sw < 0) {
906 pr_err("othc resource:IRQs absent\n");
907 rc = -ENXIO;
908 goto fail_micbias_config;
909 }
910
911 if (pdata->hsed_name != NULL)
912 ipd->name = pdata->hsed_name;
913 else
914 ipd->name = "pmic8058_othc";
915
916 ipd->phys = "pmic8058_othc/input0";
917 ipd->dev.parent = &pd->dev;
918
919 dd->othc_ipd = ipd;
920 dd->ir_gpio = hsed_config->ir_gpio;
921 dd->othc_sw_state = false;
922 dd->switch_debounce_ms = hsed_config->switch_debounce_ms;
923 dd->othc_support_n_switch = hsed_config->othc_support_n_switch;
924 dd->accessory_support = pdata->hsed_config->accessories_support;
925 dd->detection_delay_ms = pdata->hsed_config->detection_delay_ms;
926
927 if (dd->othc_support_n_switch == true)
928 dd->switch_config = hsed_config->switch_config;
929
930 if (dd->accessory_support == true) {
931 dd->accessory_info = pdata->hsed_config->accessories;
932 dd->num_accessories = pdata->hsed_config->othc_num_accessories;
933 dd->accessories_adc_support =
934 pdata->hsed_config->accessories_adc_support;
935 dd->accessories_adc_channel =
936 pdata->hsed_config->accessories_adc_channel;
937 dd->video_out_gpio = pdata->hsed_config->video_out_gpio;
938 }
939
940 /* Configure the MIC_BIAS line for headset detection */
941 rc = pm8058_configure_micbias(dd);
942 if (rc < 0)
943 goto fail_micbias_config;
944
945 /* Configure for the switch events */
946 rc = pm8058_configure_switch(dd);
947 if (rc < 0)
948 goto fail_micbias_config;
949
950 /* Configure the accessory */
951 if (dd->accessory_support == true) {
952 rc = pm8058_configure_accessory(dd);
953 if (rc < 0)
954 goto fail_micbias_config;
955 }
956
957 input_set_drvdata(ipd, dd);
958 spin_lock_init(&dd->lock);
959
960 rc = input_register_device(ipd);
961 if (rc) {
962 pr_err("Unable to register OTHC device\n");
963 goto fail_micbias_config;
964 }
965
966 hrtimer_init(&dd->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
967 dd->timer.function = pm8058_othc_timer;
968
969 /* Request the HEADSET IR interrupt */
970 if (dd->ir_gpio < 0) {
971 rc = request_threaded_irq(dd->othc_irq_ir, NULL, pm8058_nc_ir,
972 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING | IRQF_DISABLED,
973 "pm8058_othc_ir", dd);
974 if (rc < 0) {
975 pr_err("Unable to request pm8058_othc_ir IRQ\n");
976 goto fail_ir_irq;
977 }
978 } else {
979 rc = gpio_request(dd->ir_gpio, "othc_ir_gpio");
980 if (rc) {
981 pr_err("Unable to request IR GPIO\n");
982 goto fail_ir_gpio_req;
983 }
984 rc = gpio_direction_input(dd->ir_gpio);
985 if (rc) {
986 pr_err("GPIO %d set_direction failed\n", dd->ir_gpio);
987 goto fail_ir_irq;
988 }
989 dd->othc_irq_ir = gpio_to_irq(dd->ir_gpio);
990 rc = request_any_context_irq(dd->othc_irq_ir, ir_gpio_irq,
991 IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING,
992 "othc_gpio_ir_irq", dd);
993 if (rc < 0) {
994 pr_err("could not request hs irq err=%d\n", rc);
995 goto fail_ir_irq;
996 }
997 }
998 /* Request the SWITCH press/release interrupt */
999 rc = request_threaded_irq(dd->othc_irq_sw, NULL, pm8058_no_sw,
1000 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING | IRQF_DISABLED,
1001 "pm8058_othc_sw", dd);
1002 if (rc < 0) {
1003 pr_err("Unable to request pm8058_othc_sw IRQ\n");
1004 goto fail_sw_irq;
1005 }
1006
1007 /* Check if the accessory is already inserted during boot up */
1008 if (dd->ir_gpio < 0) {
1009 rc = pm8058_irq_get_rt_status(dd->pm_chip, dd->othc_irq_ir);
1010 if (rc < 0) {
1011 pr_err("Unable to get accessory status at boot\n");
1012 goto fail_ir_status;
1013 }
1014 } else {
1015 rc = gpio_get_value_cansleep(dd->ir_gpio);
1016 if (rc < 0) {
1017 pr_err("Unable to get accessory status at boot\n");
1018 goto fail_ir_status;
1019 }
1020 rc = !rc;
1021 }
1022 if (rc) {
1023 pr_debug("Accessory inserted during boot up\n");
1024 /* process the data and report the inserted accessory */
1025 rc = pm8058_accessory_report(dd, 1);
1026 if (rc)
1027 pr_debug("Unabele to detect accessory at boot up\n");
1028 }
1029
1030 device_init_wakeup(&pd->dev,
1031 hsed_config->hsed_bias_config->othc_wakeup);
1032
1033 INIT_DELAYED_WORK(&dd->detect_work, detect_work_f);
1034
1035 INIT_DELAYED_WORK(&dd->hs_work, hs_worker);
1036
1037 if (dd->othc_support_n_switch == true)
1038 INIT_WORK(&dd->switch_work, switch_work_f);
1039
1040
1041 return 0;
1042
1043fail_ir_status:
1044 free_irq(dd->othc_irq_sw, dd);
1045fail_sw_irq:
1046 free_irq(dd->othc_irq_ir, dd);
1047fail_ir_irq:
1048 if (dd->ir_gpio != -1)
1049 gpio_free(dd->ir_gpio);
1050fail_ir_gpio_req:
1051 input_unregister_device(ipd);
1052 dd->othc_ipd = NULL;
1053fail_micbias_config:
1054 input_free_device(ipd);
1055fail_input_alloc:
1056 switch_dev_unregister(&dd->othc_sdev);
1057 return rc;
1058}
1059
1060static int __devinit pm8058_othc_probe(struct platform_device *pd)
1061{
1062 int rc;
1063 struct pm8058_othc *dd;
1064 struct pm8058_chip *chip;
1065 struct resource *res;
1066 struct pmic8058_othc_config_pdata *pdata = pd->dev.platform_data;
1067
1068 chip = dev_get_drvdata(pd->dev.parent);
1069 if (chip == NULL) {
1070 pr_err("Invalid driver information\n");
1071 return -EINVAL;
1072 }
1073
1074 /* Check PMIC8058 version. A0 version is not supported */
1075 if (pm8058_rev(chip) == PM_8058_REV_1p0) {
1076 pr_err("PMIC8058 version not supported\n");
1077 return -ENODEV;
1078 }
1079
1080 if (pdata == NULL) {
1081 pr_err("Platform data not present\n");
1082 return -EINVAL;
1083 }
1084
1085 dd = kzalloc(sizeof(*dd), GFP_KERNEL);
1086 if (dd == NULL) {
1087 pr_err("Unable to allocate memory\n");
1088 return -ENOMEM;
1089 }
1090
1091 /* Enable runtime PM ops, start in ACTIVE mode */
1092 rc = pm_runtime_set_active(&pd->dev);
1093 if (rc < 0)
1094 dev_dbg(&pd->dev, "unable to set runtime pm state\n");
1095 pm_runtime_enable(&pd->dev);
1096
1097 res = platform_get_resource_byname(pd, IORESOURCE_IO, "othc_base");
1098 if (res == NULL) {
1099 pr_err("othc resource:Base address absent\n");
1100 rc = -ENXIO;
1101 goto fail_get_res;
1102 }
1103
1104 dd->othc_pdata = pdata;
1105 dd->pm_chip = chip;
1106 dd->othc_base = res->start;
1107 if (pdata->micbias_regulator == NULL) {
1108 pr_err("OTHC regulator not specified\n");
1109 goto fail_get_res;
1110 }
1111
1112 dd->othc_vreg = regulator_get(NULL,
1113 pdata->micbias_regulator->regulator);
1114 if (IS_ERR(dd->othc_vreg)) {
1115 pr_err("regulator get failed\n");
1116 rc = PTR_ERR(dd->othc_vreg);
1117 goto fail_get_res;
1118 }
1119
1120 rc = regulator_set_voltage(dd->othc_vreg,
1121 pdata->micbias_regulator->min_uV,
1122 pdata->micbias_regulator->max_uV);
1123 if (rc) {
1124 pr_err("othc regulator set voltage failed\n");
1125 goto fail_reg_enable;
1126 }
1127
1128 rc = regulator_enable(dd->othc_vreg);
1129 if (rc) {
1130 pr_err("othc regulator enable failed\n");
1131 goto fail_reg_enable;
1132 }
1133
1134 platform_set_drvdata(pd, dd);
1135
1136 if (pdata->micbias_capability == OTHC_MICBIAS_HSED) {
1137 /* HSED to be supported on this MICBIAS line */
1138 if (pdata->hsed_config != NULL) {
1139 rc = othc_configure_hsed(dd, pd);
1140 if (rc < 0)
1141 goto fail_othc_hsed;
1142 } else {
1143 pr_err("HSED config data not present\n");
1144 rc = -EINVAL;
1145 goto fail_othc_hsed;
1146 }
1147 }
1148
1149 /* Store the local driver data structure */
1150 if (dd->othc_pdata->micbias_select < OTHC_MICBIAS_MAX)
1151 config[dd->othc_pdata->micbias_select] = dd;
1152
1153 pr_debug("Device %s:%d successfully registered\n",
1154 pd->name, pd->id);
1155 return 0;
1156
1157fail_othc_hsed:
1158 regulator_disable(dd->othc_vreg);
1159fail_reg_enable:
1160 regulator_put(dd->othc_vreg);
1161fail_get_res:
1162 pm_runtime_set_suspended(&pd->dev);
1163 pm_runtime_disable(&pd->dev);
1164
1165 kfree(dd);
1166 return rc;
1167}
1168
1169static struct platform_driver pm8058_othc_driver = {
1170 .driver = {
1171 .name = "pm8058-othc",
1172 .owner = THIS_MODULE,
1173#ifdef CONFIG_PM
1174 .pm = &pm8058_othc_pm_ops,
1175#endif
1176 },
1177 .probe = pm8058_othc_probe,
1178 .remove = __devexit_p(pm8058_othc_remove),
1179};
1180
1181static int __init pm8058_othc_init(void)
1182{
1183 return platform_driver_register(&pm8058_othc_driver);
1184}
1185
1186static void __exit pm8058_othc_exit(void)
1187{
1188 platform_driver_unregister(&pm8058_othc_driver);
1189}
1190/*
1191 * Move to late_initcall, to make sure that the ADC driver registration is
1192 * completed before we open a ADC channel.
1193 */
1194late_initcall(pm8058_othc_init);
1195module_exit(pm8058_othc_exit);
1196
1197MODULE_ALIAS("platform:pmic8058_othc");
1198MODULE_DESCRIPTION("PMIC 8058 OTHC");
1199MODULE_LICENSE("GPL v2");