Merge tag 'v3.14.25' into backport/v3.14.24-ltsi-rc1+v3.14.25/snapshot-merge.wip
[platform/adaptation/renesas_rcar/renesas_kernel.git] / drivers / mfd / twl4030-madc.c
1 /*
2  *
3  * TWL4030 MADC module driver-This driver monitors the real time
4  * conversion of analog signals like battery temperature,
5  * battery type, battery level etc.
6  *
7  * Copyright (C) 2011 Texas Instruments Incorporated - http://www.ti.com/
8  * J Keerthy <j-keerthy@ti.com>
9  *
10  * Based on twl4030-madc.c
11  * Copyright (C) 2008 Nokia Corporation
12  * Mikko Ylinen <mikko.k.ylinen@nokia.com>
13  *
14  * Amit Kucheria <amit.kucheria@canonical.com>
15  *
16  * This program is free software; you can redistribute it and/or
17  * modify it under the terms of the GNU General Public License
18  * version 2 as published by the Free Software Foundation.
19  *
20  * This program is distributed in the hope that it will be useful, but
21  * WITHOUT ANY WARRANTY; without even the implied warranty of
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
23  * General Public License for more details.
24  *
25  * You should have received a copy of the GNU General Public License
26  * along with this program; if not, write to the Free Software
27  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
28  * 02110-1301 USA
29  *
30  */
31
32 #include <linux/device.h>
33 #include <linux/interrupt.h>
34 #include <linux/kernel.h>
35 #include <linux/delay.h>
36 #include <linux/platform_device.h>
37 #include <linux/slab.h>
38 #include <linux/i2c/twl.h>
39 #include <linux/i2c/twl4030-madc.h>
40 #include <linux/module.h>
41 #include <linux/stddef.h>
42 #include <linux/mutex.h>
43 #include <linux/bitops.h>
44 #include <linux/jiffies.h>
45 #include <linux/types.h>
46 #include <linux/gfp.h>
47 #include <linux/err.h>
48
49 /*
50  * struct twl4030_madc_data - a container for madc info
51  * @dev - pointer to device structure for madc
52  * @lock - mutex protecting this data structure
53  * @requests - Array of request struct corresponding to SW1, SW2 and RT
54  * @imr - Interrupt mask register of MADC
55  * @isr - Interrupt status register of MADC
56  */
57 struct twl4030_madc_data {
58         struct device *dev;
59         struct mutex lock;      /* mutex protecting this data structure */
60         struct twl4030_madc_request requests[TWL4030_MADC_NUM_METHODS];
61         int imr;
62         int isr;
63 };
64
65 static struct twl4030_madc_data *twl4030_madc;
66
67 struct twl4030_prescale_divider_ratios {
68         s16 numerator;
69         s16 denominator;
70 };
71
72 static const struct twl4030_prescale_divider_ratios
73 twl4030_divider_ratios[16] = {
74         {1, 1},         /* CHANNEL 0 No Prescaler */
75         {1, 1},         /* CHANNEL 1 No Prescaler */
76         {6, 10},        /* CHANNEL 2 */
77         {6, 10},        /* CHANNEL 3 */
78         {6, 10},        /* CHANNEL 4 */
79         {6, 10},        /* CHANNEL 5 */
80         {6, 10},        /* CHANNEL 6 */
81         {6, 10},        /* CHANNEL 7 */
82         {3, 14},        /* CHANNEL 8 */
83         {1, 3},         /* CHANNEL 9 */
84         {1, 1},         /* CHANNEL 10 No Prescaler */
85         {15, 100},      /* CHANNEL 11 */
86         {1, 4},         /* CHANNEL 12 */
87         {1, 1},         /* CHANNEL 13 Reserved channels */
88         {1, 1},         /* CHANNEL 14 Reseved channels */
89         {5, 11},        /* CHANNEL 15 */
90 };
91
92
93 /*
94  * Conversion table from -3 to 55 degree Celcius
95  */
96 static int therm_tbl[] = {
97 30800,  29500,  28300,  27100,
98 26000,  24900,  23900,  22900,  22000,  21100,  20300,  19400,  18700,  17900,
99 17200,  16500,  15900,  15300,  14700,  14100,  13600,  13100,  12600,  12100,
100 11600,  11200,  10800,  10400,  10000,  9630,   9280,   8950,   8620,   8310,
101 8020,   7730,   7460,   7200,   6950,   6710,   6470,   6250,   6040,   5830,
102 5640,   5450,   5260,   5090,   4920,   4760,   4600,   4450,   4310,   4170,
103 4040,   3910,   3790,   3670,   3550
104 };
105
106 /*
107  * Structure containing the registers
108  * of different conversion methods supported by MADC.
109  * Hardware or RT real time conversion request initiated by external host
110  * processor for RT Signal conversions.
111  * External host processors can also request for non RT conversions
112  * SW1 and SW2 software conversions also called asynchronous or GPC request.
113  */
114 static
115 const struct twl4030_madc_conversion_method twl4030_conversion_methods[] = {
116         [TWL4030_MADC_RT] = {
117                              .sel = TWL4030_MADC_RTSELECT_LSB,
118                              .avg = TWL4030_MADC_RTAVERAGE_LSB,
119                              .rbase = TWL4030_MADC_RTCH0_LSB,
120                              },
121         [TWL4030_MADC_SW1] = {
122                               .sel = TWL4030_MADC_SW1SELECT_LSB,
123                               .avg = TWL4030_MADC_SW1AVERAGE_LSB,
124                               .rbase = TWL4030_MADC_GPCH0_LSB,
125                               .ctrl = TWL4030_MADC_CTRL_SW1,
126                               },
127         [TWL4030_MADC_SW2] = {
128                               .sel = TWL4030_MADC_SW2SELECT_LSB,
129                               .avg = TWL4030_MADC_SW2AVERAGE_LSB,
130                               .rbase = TWL4030_MADC_GPCH0_LSB,
131                               .ctrl = TWL4030_MADC_CTRL_SW2,
132                               },
133 };
134
135 /*
136  * Function to read a particular channel value.
137  * @madc - pointer to struct twl4030_madc_data
138  * @reg - lsb of ADC Channel
139  * If the i2c read fails it returns an error else returns 0.
140  */
141 static int twl4030_madc_channel_raw_read(struct twl4030_madc_data *madc, u8 reg)
142 {
143         u8 msb, lsb;
144         int ret;
145         /*
146          * For each ADC channel, we have MSB and LSB register pair. MSB address
147          * is always LSB address+1. reg parameter is the address of LSB register
148          */
149         ret = twl_i2c_read_u8(TWL4030_MODULE_MADC, &msb, reg + 1);
150         if (ret) {
151                 dev_err(madc->dev, "unable to read MSB register 0x%X\n",
152                         reg + 1);
153                 return ret;
154         }
155         ret = twl_i2c_read_u8(TWL4030_MODULE_MADC, &lsb, reg);
156         if (ret) {
157                 dev_err(madc->dev, "unable to read LSB register 0x%X\n", reg);
158                 return ret;
159         }
160
161         return (int)(((msb << 8) | lsb) >> 6);
162 }
163
164 /*
165  * Return battery temperature
166  * Or < 0 on failure.
167  */
168 static int twl4030battery_temperature(int raw_volt)
169 {
170         u8 val;
171         int temp, curr, volt, res, ret;
172
173         volt = (raw_volt * TEMP_STEP_SIZE) / TEMP_PSR_R;
174         /* Getting and calculating the supply current in micro ampers */
175         ret = twl_i2c_read_u8(TWL_MODULE_MAIN_CHARGE, &val,
176                 REG_BCICTL2);
177         if (ret < 0)
178                 return ret;
179         curr = ((val & TWL4030_BCI_ITHEN) + 1) * 10;
180         /* Getting and calculating the thermistor resistance in ohms */
181         res = volt * 1000 / curr;
182         /* calculating temperature */
183         for (temp = 58; temp >= 0; temp--) {
184                 int actual = therm_tbl[temp];
185
186                 if ((actual - res) >= 0)
187                         break;
188         }
189
190         return temp + 1;
191 }
192
193 static int twl4030battery_current(int raw_volt)
194 {
195         int ret;
196         u8 val;
197
198         ret = twl_i2c_read_u8(TWL_MODULE_MAIN_CHARGE, &val,
199                 TWL4030_BCI_BCICTL1);
200         if (ret)
201                 return ret;
202         if (val & TWL4030_BCI_CGAIN) /* slope of 0.44 mV/mA */
203                 return (raw_volt * CURR_STEP_SIZE) / CURR_PSR_R1;
204         else /* slope of 0.88 mV/mA */
205                 return (raw_volt * CURR_STEP_SIZE) / CURR_PSR_R2;
206 }
207 /*
208  * Function to read channel values
209  * @madc - pointer to twl4030_madc_data struct
210  * @reg_base - Base address of the first channel
211  * @Channels - 16 bit bitmap. If the bit is set, channel value is read
212  * @buf - The channel values are stored here. if read fails error
213  * @raw - Return raw values without conversion
214  * value is stored
215  * Returns the number of successfully read channels.
216  */
217 static int twl4030_madc_read_channels(struct twl4030_madc_data *madc,
218                                       u8 reg_base, unsigned
219                                       long channels, int *buf,
220                                       bool raw)
221 {
222         int count = 0, count_req = 0, i;
223         u8 reg;
224
225         for_each_set_bit(i, &channels, TWL4030_MADC_MAX_CHANNELS) {
226                 reg = reg_base + 2 * i;
227                 buf[i] = twl4030_madc_channel_raw_read(madc, reg);
228                 if (buf[i] < 0) {
229                         dev_err(madc->dev,
230                                 "Unable to read register 0x%X\n", reg);
231                         count_req++;
232                         continue;
233                 }
234                 if (raw) {
235                         count++;
236                         continue;
237                 }
238                 switch (i) {
239                 case 10:
240                         buf[i] = twl4030battery_current(buf[i]);
241                         if (buf[i] < 0) {
242                                 dev_err(madc->dev, "err reading current\n");
243                                 count_req++;
244                         } else {
245                                 count++;
246                                 buf[i] = buf[i] - 750;
247                         }
248                         break;
249                 case 1:
250                         buf[i] = twl4030battery_temperature(buf[i]);
251                         if (buf[i] < 0) {
252                                 dev_err(madc->dev, "err reading temperature\n");
253                                 count_req++;
254                         } else {
255                                 buf[i] -= 3;
256                                 count++;
257                         }
258                         break;
259                 default:
260                         count++;
261                         /* Analog Input (V) = conv_result * step_size / R
262                          * conv_result = decimal value of 10-bit conversion
263                          *               result
264                          * step size = 1.5 / (2 ^ 10 -1)
265                          * R = Prescaler ratio for input channels.
266                          * Result given in mV hence multiplied by 1000.
267                          */
268                         buf[i] = (buf[i] * 3 * 1000 *
269                                  twl4030_divider_ratios[i].denominator)
270                                 / (2 * 1023 *
271                                 twl4030_divider_ratios[i].numerator);
272                 }
273         }
274         if (count_req)
275                 dev_err(madc->dev, "%d channel conversion failed\n", count_req);
276
277         return count;
278 }
279
280 /*
281  * Enables irq.
282  * @madc - pointer to twl4030_madc_data struct
283  * @id - irq number to be enabled
284  * can take one of TWL4030_MADC_RT, TWL4030_MADC_SW1, TWL4030_MADC_SW2
285  * corresponding to RT, SW1, SW2 conversion requests.
286  * If the i2c read fails it returns an error else returns 0.
287  */
288 static int twl4030_madc_enable_irq(struct twl4030_madc_data *madc, u8 id)
289 {
290         u8 val;
291         int ret;
292
293         ret = twl_i2c_read_u8(TWL4030_MODULE_MADC, &val, madc->imr);
294         if (ret) {
295                 dev_err(madc->dev, "unable to read imr register 0x%X\n",
296                         madc->imr);
297                 return ret;
298         }
299         val &= ~(1 << id);
300         ret = twl_i2c_write_u8(TWL4030_MODULE_MADC, val, madc->imr);
301         if (ret) {
302                 dev_err(madc->dev,
303                         "unable to write imr register 0x%X\n", madc->imr);
304                 return ret;
305
306         }
307
308         return 0;
309 }
310
311 /*
312  * Disables irq.
313  * @madc - pointer to twl4030_madc_data struct
314  * @id - irq number to be disabled
315  * can take one of TWL4030_MADC_RT, TWL4030_MADC_SW1, TWL4030_MADC_SW2
316  * corresponding to RT, SW1, SW2 conversion requests.
317  * Returns error if i2c read/write fails.
318  */
319 static int twl4030_madc_disable_irq(struct twl4030_madc_data *madc, u8 id)
320 {
321         u8 val;
322         int ret;
323
324         ret = twl_i2c_read_u8(TWL4030_MODULE_MADC, &val, madc->imr);
325         if (ret) {
326                 dev_err(madc->dev, "unable to read imr register 0x%X\n",
327                         madc->imr);
328                 return ret;
329         }
330         val |= (1 << id);
331         ret = twl_i2c_write_u8(TWL4030_MODULE_MADC, val, madc->imr);
332         if (ret) {
333                 dev_err(madc->dev,
334                         "unable to write imr register 0x%X\n", madc->imr);
335                 return ret;
336         }
337
338         return 0;
339 }
340
341 static irqreturn_t twl4030_madc_threaded_irq_handler(int irq, void *_madc)
342 {
343         struct twl4030_madc_data *madc = _madc;
344         const struct twl4030_madc_conversion_method *method;
345         u8 isr_val, imr_val;
346         int i, len, ret;
347         struct twl4030_madc_request *r;
348
349         mutex_lock(&madc->lock);
350         ret = twl_i2c_read_u8(TWL4030_MODULE_MADC, &isr_val, madc->isr);
351         if (ret) {
352                 dev_err(madc->dev, "unable to read isr register 0x%X\n",
353                         madc->isr);
354                 goto err_i2c;
355         }
356         ret = twl_i2c_read_u8(TWL4030_MODULE_MADC, &imr_val, madc->imr);
357         if (ret) {
358                 dev_err(madc->dev, "unable to read imr register 0x%X\n",
359                         madc->imr);
360                 goto err_i2c;
361         }
362         isr_val &= ~imr_val;
363         for (i = 0; i < TWL4030_MADC_NUM_METHODS; i++) {
364                 if (!(isr_val & (1 << i)))
365                         continue;
366                 ret = twl4030_madc_disable_irq(madc, i);
367                 if (ret < 0)
368                         dev_dbg(madc->dev, "Disable interrupt failed%d\n", i);
369                 madc->requests[i].result_pending = 1;
370         }
371         for (i = 0; i < TWL4030_MADC_NUM_METHODS; i++) {
372                 r = &madc->requests[i];
373                 /* No pending results for this method, move to next one */
374                 if (!r->result_pending)
375                         continue;
376                 method = &twl4030_conversion_methods[r->method];
377                 /* Read results */
378                 len = twl4030_madc_read_channels(madc, method->rbase,
379                                                  r->channels, r->rbuf, r->raw);
380                 /* Return results to caller */
381                 if (r->func_cb != NULL) {
382                         r->func_cb(len, r->channels, r->rbuf);
383                         r->func_cb = NULL;
384                 }
385                 /* Free request */
386                 r->result_pending = 0;
387                 r->active = 0;
388         }
389         mutex_unlock(&madc->lock);
390
391         return IRQ_HANDLED;
392
393 err_i2c:
394         /*
395          * In case of error check whichever request is active
396          * and service the same.
397          */
398         for (i = 0; i < TWL4030_MADC_NUM_METHODS; i++) {
399                 r = &madc->requests[i];
400                 if (r->active == 0)
401                         continue;
402                 method = &twl4030_conversion_methods[r->method];
403                 /* Read results */
404                 len = twl4030_madc_read_channels(madc, method->rbase,
405                                                  r->channels, r->rbuf, r->raw);
406                 /* Return results to caller */
407                 if (r->func_cb != NULL) {
408                         r->func_cb(len, r->channels, r->rbuf);
409                         r->func_cb = NULL;
410                 }
411                 /* Free request */
412                 r->result_pending = 0;
413                 r->active = 0;
414         }
415         mutex_unlock(&madc->lock);
416
417         return IRQ_HANDLED;
418 }
419
420 static int twl4030_madc_set_irq(struct twl4030_madc_data *madc,
421                                 struct twl4030_madc_request *req)
422 {
423         struct twl4030_madc_request *p;
424         int ret;
425
426         p = &madc->requests[req->method];
427         memcpy(p, req, sizeof(*req));
428         ret = twl4030_madc_enable_irq(madc, req->method);
429         if (ret < 0) {
430                 dev_err(madc->dev, "enable irq failed!!\n");
431                 return ret;
432         }
433
434         return 0;
435 }
436
437 /*
438  * Function which enables the madc conversion
439  * by writing to the control register.
440  * @madc - pointer to twl4030_madc_data struct
441  * @conv_method - can be TWL4030_MADC_RT, TWL4030_MADC_SW2, TWL4030_MADC_SW1
442  * corresponding to RT SW1 or SW2 conversion methods.
443  * Returns 0 if succeeds else a negative error value
444  */
445 static int twl4030_madc_start_conversion(struct twl4030_madc_data *madc,
446                                          int conv_method)
447 {
448         const struct twl4030_madc_conversion_method *method;
449         int ret = 0;
450         method = &twl4030_conversion_methods[conv_method];
451         switch (conv_method) {
452         case TWL4030_MADC_SW1:
453         case TWL4030_MADC_SW2:
454                 ret = twl_i2c_write_u8(TWL4030_MODULE_MADC,
455                                        TWL4030_MADC_SW_START, method->ctrl);
456                 if (ret) {
457                         dev_err(madc->dev,
458                                 "unable to write ctrl register 0x%X\n",
459                                 method->ctrl);
460                         return ret;
461                 }
462                 break;
463         default:
464                 break;
465         }
466
467         return 0;
468 }
469
470 /*
471  * Function that waits for conversion to be ready
472  * @madc - pointer to twl4030_madc_data struct
473  * @timeout_ms - timeout value in milliseconds
474  * @status_reg - ctrl register
475  * returns 0 if succeeds else a negative error value
476  */
477 static int twl4030_madc_wait_conversion_ready(struct twl4030_madc_data *madc,
478                                               unsigned int timeout_ms,
479                                               u8 status_reg)
480 {
481         unsigned long timeout;
482         int ret;
483
484         timeout = jiffies + msecs_to_jiffies(timeout_ms);
485         do {
486                 u8 reg;
487
488                 ret = twl_i2c_read_u8(TWL4030_MODULE_MADC, &reg, status_reg);
489                 if (ret) {
490                         dev_err(madc->dev,
491                                 "unable to read status register 0x%X\n",
492                                 status_reg);
493                         return ret;
494                 }
495                 if (!(reg & TWL4030_MADC_BUSY) && (reg & TWL4030_MADC_EOC_SW))
496                         return 0;
497                 usleep_range(500, 2000);
498         } while (!time_after(jiffies, timeout));
499         dev_err(madc->dev, "conversion timeout!\n");
500
501         return -EAGAIN;
502 }
503
504 /*
505  * An exported function which can be called from other kernel drivers.
506  * @req twl4030_madc_request structure
507  * req->rbuf will be filled with read values of channels based on the
508  * channel index. If a particular channel reading fails there will
509  * be a negative error value in the corresponding array element.
510  * returns 0 if succeeds else error value
511  */
512 int twl4030_madc_conversion(struct twl4030_madc_request *req)
513 {
514         const struct twl4030_madc_conversion_method *method;
515         u8 ch_msb, ch_lsb;
516         int ret;
517
518         if (!req || !twl4030_madc)
519                 return -EINVAL;
520
521         mutex_lock(&twl4030_madc->lock);
522         if (req->method < TWL4030_MADC_RT || req->method > TWL4030_MADC_SW2) {
523                 ret = -EINVAL;
524                 goto out;
525         }
526         /* Do we have a conversion request ongoing */
527         if (twl4030_madc->requests[req->method].active) {
528                 ret = -EBUSY;
529                 goto out;
530         }
531         ch_msb = (req->channels >> 8) & 0xff;
532         ch_lsb = req->channels & 0xff;
533         method = &twl4030_conversion_methods[req->method];
534         /* Select channels to be converted */
535         ret = twl_i2c_write_u8(TWL4030_MODULE_MADC, ch_msb, method->sel + 1);
536         if (ret) {
537                 dev_err(twl4030_madc->dev,
538                         "unable to write sel register 0x%X\n", method->sel + 1);
539                 goto out;
540         }
541         ret = twl_i2c_write_u8(TWL4030_MODULE_MADC, ch_lsb, method->sel);
542         if (ret) {
543                 dev_err(twl4030_madc->dev,
544                         "unable to write sel register 0x%X\n", method->sel + 1);
545                 goto out;
546         }
547         /* Select averaging for all channels if do_avg is set */
548         if (req->do_avg) {
549                 ret = twl_i2c_write_u8(TWL4030_MODULE_MADC,
550                                        ch_msb, method->avg + 1);
551                 if (ret) {
552                         dev_err(twl4030_madc->dev,
553                                 "unable to write avg register 0x%X\n",
554                                 method->avg + 1);
555                         goto out;
556                 }
557                 ret = twl_i2c_write_u8(TWL4030_MODULE_MADC,
558                                        ch_lsb, method->avg);
559                 if (ret) {
560                         dev_err(twl4030_madc->dev,
561                                 "unable to write sel reg 0x%X\n",
562                                 method->sel + 1);
563                         goto out;
564                 }
565         }
566         if (req->type == TWL4030_MADC_IRQ_ONESHOT && req->func_cb != NULL) {
567                 ret = twl4030_madc_set_irq(twl4030_madc, req);
568                 if (ret < 0)
569                         goto out;
570                 ret = twl4030_madc_start_conversion(twl4030_madc, req->method);
571                 if (ret < 0)
572                         goto out;
573                 twl4030_madc->requests[req->method].active = 1;
574                 ret = 0;
575                 goto out;
576         }
577         /* With RT method we should not be here anymore */
578         if (req->method == TWL4030_MADC_RT) {
579                 ret = -EINVAL;
580                 goto out;
581         }
582         ret = twl4030_madc_start_conversion(twl4030_madc, req->method);
583         if (ret < 0)
584                 goto out;
585         twl4030_madc->requests[req->method].active = 1;
586         /* Wait until conversion is ready (ctrl register returns EOC) */
587         ret = twl4030_madc_wait_conversion_ready(twl4030_madc, 5, method->ctrl);
588         if (ret) {
589                 twl4030_madc->requests[req->method].active = 0;
590                 goto out;
591         }
592         ret = twl4030_madc_read_channels(twl4030_madc, method->rbase,
593                                          req->channels, req->rbuf, req->raw);
594         twl4030_madc->requests[req->method].active = 0;
595
596 out:
597         mutex_unlock(&twl4030_madc->lock);
598
599         return ret;
600 }
601 EXPORT_SYMBOL_GPL(twl4030_madc_conversion);
602
603 /*
604  * Return channel value
605  * Or < 0 on failure.
606  */
607 int twl4030_get_madc_conversion(int channel_no)
608 {
609         struct twl4030_madc_request req;
610         int temp = 0;
611         int ret;
612
613         req.channels = (1 << channel_no);
614         req.method = TWL4030_MADC_SW2;
615         req.active = 0;
616         req.func_cb = NULL;
617         ret = twl4030_madc_conversion(&req);
618         if (ret < 0)
619                 return ret;
620         if (req.rbuf[channel_no] > 0)
621                 temp = req.rbuf[channel_no];
622
623         return temp;
624 }
625 EXPORT_SYMBOL_GPL(twl4030_get_madc_conversion);
626
627 /*
628  * Function to enable or disable bias current for
629  * main battery type reading or temperature sensing
630  * @madc - pointer to twl4030_madc_data struct
631  * @chan - can be one of the two values
632  * TWL4030_BCI_ITHEN - Enables bias current for main battery type reading
633  * TWL4030_BCI_TYPEN - Enables bias current for main battery temperature
634  * sensing
635  * @on - enable or disable chan.
636  */
637 static int twl4030_madc_set_current_generator(struct twl4030_madc_data *madc,
638                                               int chan, int on)
639 {
640         int ret;
641         u8 regval;
642
643         ret = twl_i2c_read_u8(TWL_MODULE_MAIN_CHARGE,
644                               &regval, TWL4030_BCI_BCICTL1);
645         if (ret) {
646                 dev_err(madc->dev, "unable to read BCICTL1 reg 0x%X",
647                         TWL4030_BCI_BCICTL1);
648                 return ret;
649         }
650         if (on)
651                 regval |= chan ? TWL4030_BCI_ITHEN : TWL4030_BCI_TYPEN;
652         else
653                 regval &= chan ? ~TWL4030_BCI_ITHEN : ~TWL4030_BCI_TYPEN;
654         ret = twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE,
655                                regval, TWL4030_BCI_BCICTL1);
656         if (ret) {
657                 dev_err(madc->dev, "unable to write BCICTL1 reg 0x%X\n",
658                         TWL4030_BCI_BCICTL1);
659                 return ret;
660         }
661
662         return 0;
663 }
664
665 /*
666  * Function that sets MADC software power on bit to enable MADC
667  * @madc - pointer to twl4030_madc_data struct
668  * @on - Enable or disable MADC software powen on bit.
669  * returns error if i2c read/write fails else 0
670  */
671 static int twl4030_madc_set_power(struct twl4030_madc_data *madc, int on)
672 {
673         u8 regval;
674         int ret;
675
676         ret = twl_i2c_read_u8(TWL_MODULE_MAIN_CHARGE,
677                               &regval, TWL4030_MADC_CTRL1);
678         if (ret) {
679                 dev_err(madc->dev, "unable to read madc ctrl1 reg 0x%X\n",
680                         TWL4030_MADC_CTRL1);
681                 return ret;
682         }
683         if (on)
684                 regval |= TWL4030_MADC_MADCON;
685         else
686                 regval &= ~TWL4030_MADC_MADCON;
687         ret = twl_i2c_write_u8(TWL4030_MODULE_MADC, regval, TWL4030_MADC_CTRL1);
688         if (ret) {
689                 dev_err(madc->dev, "unable to write madc ctrl1 reg 0x%X\n",
690                         TWL4030_MADC_CTRL1);
691                 return ret;
692         }
693
694         return 0;
695 }
696
697 /*
698  * Initialize MADC and request for threaded irq
699  */
700 static int twl4030_madc_probe(struct platform_device *pdev)
701 {
702         struct twl4030_madc_data *madc;
703         struct twl4030_madc_platform_data *pdata = dev_get_platdata(&pdev->dev);
704         int ret;
705         u8 regval;
706
707         if (!pdata) {
708                 dev_err(&pdev->dev, "platform_data not available\n");
709                 return -EINVAL;
710         }
711         madc = kzalloc(sizeof(*madc), GFP_KERNEL);
712         if (!madc)
713                 return -ENOMEM;
714
715         madc->dev = &pdev->dev;
716
717         /*
718          * Phoenix provides 2 interrupt lines. The first one is connected to
719          * the OMAP. The other one can be connected to the other processor such
720          * as modem. Hence two separate ISR and IMR registers.
721          */
722         madc->imr = (pdata->irq_line == 1) ?
723             TWL4030_MADC_IMR1 : TWL4030_MADC_IMR2;
724         madc->isr = (pdata->irq_line == 1) ?
725             TWL4030_MADC_ISR1 : TWL4030_MADC_ISR2;
726         ret = twl4030_madc_set_power(madc, 1);
727         if (ret < 0)
728                 goto err_power;
729         ret = twl4030_madc_set_current_generator(madc, 0, 1);
730         if (ret < 0)
731                 goto err_current_generator;
732
733         ret = twl_i2c_read_u8(TWL_MODULE_MAIN_CHARGE,
734                               &regval, TWL4030_BCI_BCICTL1);
735         if (ret) {
736                 dev_err(&pdev->dev, "unable to read reg BCI CTL1 0x%X\n",
737                         TWL4030_BCI_BCICTL1);
738                 goto err_i2c;
739         }
740         regval |= TWL4030_BCI_MESBAT;
741         ret = twl_i2c_write_u8(TWL_MODULE_MAIN_CHARGE,
742                                regval, TWL4030_BCI_BCICTL1);
743         if (ret) {
744                 dev_err(&pdev->dev, "unable to write reg BCI Ctl1 0x%X\n",
745                         TWL4030_BCI_BCICTL1);
746                 goto err_i2c;
747         }
748
749         /* Check that MADC clock is on */
750         ret = twl_i2c_read_u8(TWL4030_MODULE_INTBR, &regval, TWL4030_REG_GPBR1);
751         if (ret) {
752                 dev_err(&pdev->dev, "unable to read reg GPBR1 0x%X\n",
753                                 TWL4030_REG_GPBR1);
754                 goto err_i2c;
755         }
756
757         /* If MADC clk is not on, turn it on */
758         if (!(regval & TWL4030_GPBR1_MADC_HFCLK_EN)) {
759                 dev_info(&pdev->dev, "clk disabled, enabling\n");
760                 regval |= TWL4030_GPBR1_MADC_HFCLK_EN;
761                 ret = twl_i2c_write_u8(TWL4030_MODULE_INTBR, regval,
762                                        TWL4030_REG_GPBR1);
763                 if (ret) {
764                         dev_err(&pdev->dev, "unable to write reg GPBR1 0x%X\n",
765                                         TWL4030_REG_GPBR1);
766                         goto err_i2c;
767                 }
768         }
769
770         platform_set_drvdata(pdev, madc);
771         mutex_init(&madc->lock);
772         ret = request_threaded_irq(platform_get_irq(pdev, 0), NULL,
773                                    twl4030_madc_threaded_irq_handler,
774                                    IRQF_TRIGGER_RISING, "twl4030_madc", madc);
775         if (ret) {
776                 dev_dbg(&pdev->dev, "could not request irq\n");
777                 goto err_i2c;
778         }
779         twl4030_madc = madc;
780         return 0;
781 err_i2c:
782         twl4030_madc_set_current_generator(madc, 0, 0);
783 err_current_generator:
784         twl4030_madc_set_power(madc, 0);
785 err_power:
786         kfree(madc);
787
788         return ret;
789 }
790
791 static int twl4030_madc_remove(struct platform_device *pdev)
792 {
793         struct twl4030_madc_data *madc = platform_get_drvdata(pdev);
794
795         free_irq(platform_get_irq(pdev, 0), madc);
796         twl4030_madc_set_current_generator(madc, 0, 0);
797         twl4030_madc_set_power(madc, 0);
798         kfree(madc);
799
800         return 0;
801 }
802
803 static struct platform_driver twl4030_madc_driver = {
804         .probe = twl4030_madc_probe,
805         .remove = twl4030_madc_remove,
806         .driver = {
807                    .name = "twl4030_madc",
808                    .owner = THIS_MODULE,
809                    },
810 };
811
812 module_platform_driver(twl4030_madc_driver);
813
814 MODULE_DESCRIPTION("TWL4030 ADC driver");
815 MODULE_LICENSE("GPL");
816 MODULE_AUTHOR("J Keerthy");
817 MODULE_ALIAS("platform:twl4030_madc");