Merge tag 'asm-generic' of git://git.kernel.org/pub/scm/linux/kernel/git/arnd/asm...
[platform/adaptation/renesas_rcar/renesas_kernel.git] / drivers / regulator / da9055-regulator.c
1 /*
2 * Regulator driver for DA9055 PMIC
3 *
4 * Copyright(c) 2012 Dialog Semiconductor Ltd.
5 *
6 * Author: David Dajun Chen <dchen@diasemi.com>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 */
14
15 #include <linux/module.h>
16 #include <linux/init.h>
17 #include <linux/err.h>
18 #include <linux/gpio.h>
19 #include <linux/platform_device.h>
20 #include <linux/regulator/driver.h>
21 #include <linux/regulator/machine.h>
22
23 #include <linux/mfd/da9055/core.h>
24 #include <linux/mfd/da9055/reg.h>
25 #include <linux/mfd/da9055/pdata.h>
26
27 #define DA9055_MIN_UA           0
28 #define DA9055_MAX_UA           3
29
30 #define DA9055_LDO_MODE_SYNC    0
31 #define DA9055_LDO_MODE_SLEEP   1
32
33 #define DA9055_BUCK_MODE_SLEEP  1
34 #define DA9055_BUCK_MODE_SYNC   2
35 #define DA9055_BUCK_MODE_AUTO   3
36
37 /* DA9055 REGULATOR IDs */
38 #define DA9055_ID_BUCK1 0
39 #define DA9055_ID_BUCK2 1
40 #define DA9055_ID_LDO1          2
41 #define DA9055_ID_LDO2          3
42 #define DA9055_ID_LDO3          4
43 #define DA9055_ID_LDO4          5
44 #define DA9055_ID_LDO5          6
45 #define DA9055_ID_LDO6          7
46
47 /* DA9055 BUCK current limit */
48 static const int da9055_current_limits[] = { 500000, 600000, 700000, 800000 };
49
50 struct da9055_conf_reg {
51         int reg;
52         int sel_mask;
53         int en_mask;
54 };
55
56 struct da9055_volt_reg {
57         int reg_a;
58         int reg_b;
59         int sl_shift;
60         int v_mask;
61         int v_shift;
62 };
63
64 struct da9055_mode_reg {
65         int reg;
66         int mask;
67         int shift;
68 };
69
70 struct da9055_regulator_info {
71         struct regulator_desc reg_desc;
72         struct da9055_conf_reg conf;
73         struct da9055_volt_reg volt;
74         struct da9055_mode_reg mode;
75 };
76
77 struct da9055_regulator {
78         struct da9055 *da9055;
79         struct da9055_regulator_info *info;
80         struct regulator_dev *rdev;
81         enum gpio_select reg_rselect;
82 };
83
84 static unsigned int da9055_buck_get_mode(struct regulator_dev *rdev)
85 {
86         struct da9055_regulator *regulator = rdev_get_drvdata(rdev);
87         struct da9055_regulator_info *info = regulator->info;
88         int ret, mode = 0;
89
90         ret = da9055_reg_read(regulator->da9055, info->mode.reg);
91         if (ret < 0)
92                 return ret;
93
94         switch ((ret & info->mode.mask) >> info->mode.shift) {
95         case DA9055_BUCK_MODE_SYNC:
96                 mode = REGULATOR_MODE_FAST;
97                 break;
98         case DA9055_BUCK_MODE_AUTO:
99                 mode = REGULATOR_MODE_NORMAL;
100                 break;
101         case DA9055_BUCK_MODE_SLEEP:
102                 mode = REGULATOR_MODE_STANDBY;
103                 break;
104         }
105
106         return mode;
107 }
108
109 static int da9055_buck_set_mode(struct regulator_dev *rdev,
110                                         unsigned int mode)
111 {
112         struct da9055_regulator *regulator = rdev_get_drvdata(rdev);
113         struct da9055_regulator_info *info = regulator->info;
114         int val = 0;
115
116         switch (mode) {
117         case REGULATOR_MODE_FAST:
118                 val = DA9055_BUCK_MODE_SYNC << info->mode.shift;
119                 break;
120         case REGULATOR_MODE_NORMAL:
121                 val = DA9055_BUCK_MODE_AUTO << info->mode.shift;
122                 break;
123         case REGULATOR_MODE_STANDBY:
124                 val = DA9055_BUCK_MODE_SLEEP << info->mode.shift;
125                 break;
126         }
127
128         return da9055_reg_update(regulator->da9055, info->mode.reg,
129                                  info->mode.mask, val);
130 }
131
132 static unsigned int da9055_ldo_get_mode(struct regulator_dev *rdev)
133 {
134         struct da9055_regulator *regulator = rdev_get_drvdata(rdev);
135         struct da9055_regulator_info *info = regulator->info;
136         int ret;
137
138         ret = da9055_reg_read(regulator->da9055, info->volt.reg_b);
139         if (ret < 0)
140                 return ret;
141
142         if (ret >> info->volt.sl_shift)
143                 return REGULATOR_MODE_STANDBY;
144         else
145                 return REGULATOR_MODE_NORMAL;
146 }
147
148 static int da9055_ldo_set_mode(struct regulator_dev *rdev, unsigned int mode)
149 {
150         struct da9055_regulator *regulator = rdev_get_drvdata(rdev);
151         struct da9055_regulator_info *info = regulator->info;
152         struct da9055_volt_reg volt = info->volt;
153         int val = 0;
154
155         switch (mode) {
156         case REGULATOR_MODE_NORMAL:
157         case REGULATOR_MODE_FAST:
158                 val = DA9055_LDO_MODE_SYNC;
159                 break;
160         case REGULATOR_MODE_STANDBY:
161                 val = DA9055_LDO_MODE_SLEEP;
162                 break;
163         }
164
165         return da9055_reg_update(regulator->da9055, volt.reg_b,
166                                  1 << volt.sl_shift,
167                                  val << volt.sl_shift);
168 }
169
170 static int da9055_buck_get_current_limit(struct regulator_dev *rdev)
171 {
172         struct da9055_regulator *regulator = rdev_get_drvdata(rdev);
173         struct da9055_regulator_info *info = regulator->info;
174         int ret;
175
176         ret = da9055_reg_read(regulator->da9055, DA9055_REG_BUCK_LIM);
177         if (ret < 0)
178                 return ret;
179
180         ret &= info->mode.mask;
181         return da9055_current_limits[ret >> info->mode.shift];
182 }
183
184 static int da9055_buck_set_current_limit(struct regulator_dev *rdev, int min_uA,
185                                          int max_uA)
186 {
187         struct da9055_regulator *regulator = rdev_get_drvdata(rdev);
188         struct da9055_regulator_info *info = regulator->info;
189         int i;
190
191         for (i = ARRAY_SIZE(da9055_current_limits) - 1; i >= 0; i--) {
192                 if ((min_uA <= da9055_current_limits[i]) &&
193                     (da9055_current_limits[i] <= max_uA))
194                         return da9055_reg_update(regulator->da9055,
195                                                  DA9055_REG_BUCK_LIM,
196                                                  info->mode.mask,
197                                                  i << info->mode.shift);
198         }
199
200         return -EINVAL;
201 }
202
203 static int da9055_regulator_get_voltage_sel(struct regulator_dev *rdev)
204 {
205         struct da9055_regulator *regulator = rdev_get_drvdata(rdev);
206         struct da9055_regulator_info *info = regulator->info;
207         struct da9055_volt_reg volt = info->volt;
208         int ret, sel;
209
210         /*
211          * There are two voltage register set A & B for voltage ramping but
212          * either one of then can be active therefore we first determine
213          * the active register set.
214          */
215         ret = da9055_reg_read(regulator->da9055, info->conf.reg);
216         if (ret < 0)
217                 return ret;
218
219         ret &= info->conf.sel_mask;
220
221         /* Get the voltage for the active register set A/B */
222         if (ret == DA9055_REGUALTOR_SET_A)
223                 ret = da9055_reg_read(regulator->da9055, volt.reg_a);
224         else
225                 ret = da9055_reg_read(regulator->da9055, volt.reg_b);
226
227         if (ret < 0)
228                 return ret;
229
230         sel = (ret & volt.v_mask);
231         return sel;
232 }
233
234 static int da9055_regulator_set_voltage_sel(struct regulator_dev *rdev,
235                                             unsigned int selector)
236 {
237         struct da9055_regulator *regulator = rdev_get_drvdata(rdev);
238         struct da9055_regulator_info *info = regulator->info;
239         int ret;
240
241         /*
242          * Regulator register set A/B is not selected through GPIO therefore
243          * we use default register set A for voltage ramping.
244          */
245         if (regulator->reg_rselect == NO_GPIO) {
246                 /* Select register set A */
247                 ret = da9055_reg_update(regulator->da9055, info->conf.reg,
248                                         info->conf.sel_mask, DA9055_SEL_REG_A);
249                 if (ret < 0)
250                         return ret;
251
252                 /* Set the voltage */
253                 return da9055_reg_update(regulator->da9055, info->volt.reg_a,
254                                          info->volt.v_mask, selector);
255         }
256
257         /*
258          * Here regulator register set A/B is selected through GPIO.
259          * Therefore we first determine the selected register set A/B and
260          * then set the desired voltage for that register set A/B.
261          */
262         ret = da9055_reg_read(regulator->da9055, info->conf.reg);
263         if (ret < 0)
264                 return ret;
265
266         ret &= info->conf.sel_mask;
267
268         /* Set the voltage */
269         if (ret == DA9055_REGUALTOR_SET_A)
270                 return da9055_reg_update(regulator->da9055, info->volt.reg_a,
271                                          info->volt.v_mask, selector);
272         else
273                 return da9055_reg_update(regulator->da9055, info->volt.reg_b,
274                                          info->volt.v_mask, selector);
275 }
276
277 static int da9055_regulator_set_suspend_voltage(struct regulator_dev *rdev,
278                                                 int uV)
279 {
280         struct da9055_regulator *regulator = rdev_get_drvdata(rdev);
281         struct da9055_regulator_info *info = regulator->info;
282         int ret;
283
284         /* Select register set B for suspend voltage ramping. */
285         if (regulator->reg_rselect == NO_GPIO) {
286                 ret = da9055_reg_update(regulator->da9055, info->conf.reg,
287                                         info->conf.sel_mask, DA9055_SEL_REG_B);
288                 if (ret < 0)
289                         return ret;
290         }
291
292         ret = regulator_map_voltage_linear(rdev, uV, uV);
293         if (ret < 0)
294                 return ret;
295
296         return da9055_reg_update(regulator->da9055, info->volt.reg_b,
297                                  info->volt.v_mask, ret);
298 }
299
300 static int da9055_suspend_enable(struct regulator_dev *rdev)
301 {
302         struct da9055_regulator *regulator = rdev_get_drvdata(rdev);
303         struct da9055_regulator_info *info = regulator->info;
304
305         /* Select register set B for voltage ramping. */
306         if (regulator->reg_rselect == NO_GPIO)
307                 return da9055_reg_update(regulator->da9055, info->conf.reg,
308                                         info->conf.sel_mask, DA9055_SEL_REG_B);
309         else
310                 return 0;
311 }
312
313 static int da9055_suspend_disable(struct regulator_dev *rdev)
314 {
315         struct da9055_regulator *regulator = rdev_get_drvdata(rdev);
316         struct da9055_regulator_info *info = regulator->info;
317
318         /* Diselect register set B. */
319         if (regulator->reg_rselect == NO_GPIO)
320                 return da9055_reg_update(regulator->da9055, info->conf.reg,
321                                         info->conf.sel_mask, DA9055_SEL_REG_A);
322         else
323                 return 0;
324 }
325
326 static struct regulator_ops da9055_buck_ops = {
327         .get_mode = da9055_buck_get_mode,
328         .set_mode = da9055_buck_set_mode,
329
330         .get_current_limit = da9055_buck_get_current_limit,
331         .set_current_limit = da9055_buck_set_current_limit,
332
333         .get_voltage_sel = da9055_regulator_get_voltage_sel,
334         .set_voltage_sel = da9055_regulator_set_voltage_sel,
335         .list_voltage = regulator_list_voltage_linear,
336         .map_voltage = regulator_map_voltage_linear,
337         .is_enabled = regulator_is_enabled_regmap,
338         .enable = regulator_enable_regmap,
339         .disable = regulator_disable_regmap,
340
341         .set_suspend_voltage = da9055_regulator_set_suspend_voltage,
342         .set_suspend_enable = da9055_suspend_enable,
343         .set_suspend_disable = da9055_suspend_disable,
344         .set_suspend_mode = da9055_buck_set_mode,
345 };
346
347 static struct regulator_ops da9055_ldo_ops = {
348         .get_mode = da9055_ldo_get_mode,
349         .set_mode = da9055_ldo_set_mode,
350
351         .get_voltage_sel = da9055_regulator_get_voltage_sel,
352         .set_voltage_sel = da9055_regulator_set_voltage_sel,
353         .list_voltage = regulator_list_voltage_linear,
354         .map_voltage = regulator_map_voltage_linear,
355         .is_enabled = regulator_is_enabled_regmap,
356         .enable = regulator_enable_regmap,
357         .disable = regulator_disable_regmap,
358
359         .set_suspend_voltage = da9055_regulator_set_suspend_voltage,
360         .set_suspend_enable = da9055_suspend_enable,
361         .set_suspend_disable = da9055_suspend_disable,
362         .set_suspend_mode = da9055_ldo_set_mode,
363
364 };
365
366 #define DA9055_LDO(_id, step, min, max, vbits, voffset) \
367 {\
368         .reg_desc = {\
369                 .name = #_id,\
370                 .ops = &da9055_ldo_ops,\
371                 .type = REGULATOR_VOLTAGE,\
372                 .id = DA9055_ID_##_id,\
373                 .n_voltages = (max - min) / step + 1 + (voffset), \
374                 .enable_reg = DA9055_REG_BCORE_CONT + DA9055_ID_##_id, \
375                 .enable_mask = 1, \
376                 .min_uV = (min) * 1000,\
377                 .uV_step = (step) * 1000,\
378                 .linear_min_sel = (voffset),\
379                 .owner = THIS_MODULE,\
380         },\
381         .conf = {\
382                 .reg = DA9055_REG_BCORE_CONT + DA9055_ID_##_id, \
383                 .sel_mask = (1 << 4),\
384                 .en_mask = 1,\
385         },\
386         .volt = {\
387                 .reg_a = DA9055_REG_VBCORE_A + DA9055_ID_##_id, \
388                 .reg_b = DA9055_REG_VBCORE_B + DA9055_ID_##_id, \
389                 .sl_shift = 7,\
390                 .v_mask = (1 << (vbits)) - 1,\
391                 .v_shift = (vbits),\
392         },\
393 }
394
395 #define DA9055_BUCK(_id, step, min, max, vbits, voffset, mbits, sbits) \
396 {\
397         .reg_desc = {\
398                 .name = #_id,\
399                 .ops = &da9055_buck_ops,\
400                 .type = REGULATOR_VOLTAGE,\
401                 .id = DA9055_ID_##_id,\
402                 .n_voltages = (max - min) / step + 1 + (voffset), \
403                 .enable_reg = DA9055_REG_BCORE_CONT + DA9055_ID_##_id, \
404                 .enable_mask = 1,\
405                 .min_uV = (min) * 1000,\
406                 .uV_step = (step) * 1000,\
407                 .linear_min_sel = (voffset),\
408                 .owner = THIS_MODULE,\
409         },\
410         .conf = {\
411                 .reg = DA9055_REG_BCORE_CONT + DA9055_ID_##_id, \
412                 .sel_mask = (1 << 4),\
413                 .en_mask = 1,\
414         },\
415         .volt = {\
416                 .reg_a = DA9055_REG_VBCORE_A + DA9055_ID_##_id, \
417                 .reg_b = DA9055_REG_VBCORE_B + DA9055_ID_##_id, \
418                 .sl_shift = 7,\
419                 .v_mask = (1 << (vbits)) - 1,\
420                 .v_shift = (vbits),\
421         },\
422         .mode = {\
423                 .reg = DA9055_REG_BCORE_MODE,\
424                 .mask = (mbits),\
425                 .shift = (sbits),\
426         },\
427 }
428
429 static struct da9055_regulator_info da9055_regulator_info[] = {
430         DA9055_BUCK(BUCK1, 25, 725, 2075, 6, 9, 0xc, 2),
431         DA9055_BUCK(BUCK2, 25, 925, 2500, 6, 0, 3, 0),
432         DA9055_LDO(LDO1, 50, 900, 3300, 6, 2),
433         DA9055_LDO(LDO2, 50, 900, 3300, 6, 3),
434         DA9055_LDO(LDO3, 50, 900, 3300, 6, 2),
435         DA9055_LDO(LDO4, 50, 900, 3300, 6, 2),
436         DA9055_LDO(LDO5, 50, 900, 2750, 6, 2),
437         DA9055_LDO(LDO6, 20, 900, 3300, 7, 0),
438 };
439
440 /*
441  * Configures regulator to be controlled either through GPIO 1 or 2.
442  * GPIO can control regulator state and/or select the regulator register
443  * set A/B for voltage ramping.
444  */
445 static __devinit int da9055_gpio_init(struct da9055_regulator *regulator,
446                                       struct regulator_config *config,
447                                       struct da9055_pdata *pdata, int id)
448 {
449         struct da9055_regulator_info *info = regulator->info;
450         int ret = 0;
451
452         if (pdata->gpio_ren && pdata->gpio_ren[id]) {
453                 char name[18];
454                 int gpio_mux = pdata->gpio_ren[id];
455
456                 config->ena_gpio = pdata->ena_gpio[id];
457                 config->ena_gpio_flags = GPIOF_OUT_INIT_HIGH;
458                 config->ena_gpio_invert = 1;
459
460                 /*
461                  * GPI pin is muxed with regulator to control the
462                  * regulator state.
463                  */
464                 sprintf(name, "DA9055 GPI %d", gpio_mux);
465                 ret = devm_gpio_request_one(config->dev, gpio_mux, GPIOF_DIR_IN,
466                                             name);
467                 if (ret < 0)
468                         goto err;
469
470                 /*
471                  * Let the regulator know that its state is controlled
472                  * through GPI.
473                  */
474                 ret = da9055_reg_update(regulator->da9055, info->conf.reg,
475                                         DA9055_E_GPI_MASK,
476                                         pdata->reg_ren[id]
477                                         << DA9055_E_GPI_SHIFT);
478                 if (ret < 0)
479                         goto err;
480         }
481
482         if (pdata->gpio_rsel && pdata->gpio_rsel[id]) {
483                 char name[18];
484                 int gpio_mux = pdata->gpio_rsel[id];
485
486                 regulator->reg_rselect = pdata->reg_rsel[id];
487
488                 /*
489                  * GPI pin is muxed with regulator to select the
490                  * regulator register set A/B for voltage ramping.
491                  */
492                 sprintf(name, "DA9055 GPI %d", gpio_mux);
493                 ret = devm_gpio_request_one(config->dev, gpio_mux, GPIOF_DIR_IN,
494                                             name);
495                 if (ret < 0)
496                         goto err;
497
498                 /*
499                  * Let the regulator know that its register set A/B
500                  * will be selected through GPI for voltage ramping.
501                  */
502                 ret = da9055_reg_update(regulator->da9055, info->conf.reg,
503                                         DA9055_V_GPI_MASK,
504                                         pdata->reg_rsel[id]
505                                         << DA9055_V_GPI_SHIFT);
506         }
507
508 err:
509         return ret;
510 }
511
512 static irqreturn_t da9055_ldo5_6_oc_irq(int irq, void *data)
513 {
514         struct da9055_regulator *regulator = data;
515
516         regulator_notifier_call_chain(regulator->rdev,
517                                       REGULATOR_EVENT_OVER_CURRENT, NULL);
518
519         return IRQ_HANDLED;
520 }
521
522 static inline struct da9055_regulator_info *find_regulator_info(int id)
523 {
524         struct da9055_regulator_info *info;
525         int i;
526
527         for (i = 0; i < ARRAY_SIZE(da9055_regulator_info); i++) {
528                 info = &da9055_regulator_info[i];
529                 if (info->reg_desc.id == id)
530                         return info;
531         }
532
533         return NULL;
534 }
535
536 static int __devinit da9055_regulator_probe(struct platform_device *pdev)
537 {
538         struct regulator_config config = { };
539         struct da9055_regulator *regulator;
540         struct da9055 *da9055 = dev_get_drvdata(pdev->dev.parent);
541         struct da9055_pdata *pdata = da9055->dev->platform_data;
542         int ret, irq;
543
544         if (pdata == NULL || pdata->regulators[pdev->id] == NULL)
545                 return -ENODEV;
546
547         regulator = devm_kzalloc(&pdev->dev, sizeof(struct da9055_regulator),
548                                  GFP_KERNEL);
549         if (!regulator)
550                 return -ENOMEM;
551
552         regulator->info = find_regulator_info(pdev->id);
553         if (regulator->info == NULL) {
554                 dev_err(&pdev->dev, "invalid regulator ID specified\n");
555                 return -EINVAL;
556         }
557
558         regulator->da9055 = da9055;
559         config.dev = &pdev->dev;
560         config.driver_data = regulator;
561         config.regmap = da9055->regmap;
562
563         if (pdata && pdata->regulators)
564                 config.init_data = pdata->regulators[pdev->id];
565
566         ret = da9055_gpio_init(regulator, &config, pdata, pdev->id);
567         if (ret < 0)
568                 return ret;
569
570         regulator->rdev = regulator_register(&regulator->info->reg_desc,
571                                              &config);
572         if (IS_ERR(regulator->rdev)) {
573                 dev_err(&pdev->dev, "Failed to register regulator %s\n",
574                         regulator->info->reg_desc.name);
575                 ret = PTR_ERR(regulator->rdev);
576                 return ret;
577         }
578
579         /* Only LDO 5 and 6 has got the over current interrupt */
580         if (pdev->id == DA9055_ID_LDO5 || pdev->id ==  DA9055_ID_LDO6) {
581                 irq = platform_get_irq_byname(pdev, "REGULATOR");
582                 irq = regmap_irq_get_virq(da9055->irq_data, irq);
583                 ret = devm_request_threaded_irq(&pdev->dev, irq, NULL,
584                                                 da9055_ldo5_6_oc_irq,
585                                                 IRQF_TRIGGER_HIGH |
586                                                 IRQF_ONESHOT |
587                                                 IRQF_PROBE_SHARED,
588                                                 pdev->name, regulator);
589                 if (ret != 0) {
590                         if (ret != -EBUSY) {
591                                 dev_err(&pdev->dev,
592                                 "Failed to request Regulator IRQ %d: %d\n",
593                                 irq, ret);
594                                 goto err_regulator;
595                         }
596                 }
597         }
598
599         platform_set_drvdata(pdev, regulator);
600
601         return 0;
602
603 err_regulator:
604         regulator_unregister(regulator->rdev);
605         return ret;
606 }
607
608 static int __devexit da9055_regulator_remove(struct platform_device *pdev)
609 {
610         struct da9055_regulator *regulator = platform_get_drvdata(pdev);
611
612         regulator_unregister(regulator->rdev);
613
614         return 0;
615 }
616
617 static struct platform_driver da9055_regulator_driver = {
618         .probe = da9055_regulator_probe,
619         .remove = __devexit_p(da9055_regulator_remove),
620         .driver = {
621                 .name = "da9055-regulator",
622                 .owner = THIS_MODULE,
623         },
624 };
625
626 static int __init da9055_regulator_init(void)
627 {
628         return platform_driver_register(&da9055_regulator_driver);
629 }
630 subsys_initcall(da9055_regulator_init);
631
632 static void __exit da9055_regulator_exit(void)
633 {
634         platform_driver_unregister(&da9055_regulator_driver);
635 }
636 module_exit(da9055_regulator_exit);
637
638 MODULE_AUTHOR("David Dajun Chen <dchen@diasemi.com>");
639 MODULE_DESCRIPTION("Power Regulator driver for Dialog DA9055 PMIC");
640 MODULE_LICENSE("GPL");
641 MODULE_ALIAS("platform:da9055-regulator");