blob: e29634148ed2d8fc06c857f55587befc3bf55f6b [file] [log] [blame]
Daniel Jeongb59320c2012-12-17 10:24:06 +09001/*
2 * LP8755 High Performance Power Management Unit : System Interface Driver
3 * (based on rev. 0.26)
4 * Copyright 2012 Texas Instruments
5 *
6 * Author: Daniel(Geon Si) Jeong <daniel.jeong@ti.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 version 2 as
10 * published by the Free Software Foundation.
11 *
12 */
13
14#include <linux/module.h>
15#include <linux/slab.h>
16#include <linux/i2c.h>
17#include <linux/err.h>
18#include <linux/irq.h>
19#include <linux/interrupt.h>
20#include <linux/gpio.h>
21#include <linux/regmap.h>
22#include <linux/delay.h>
23#include <linux/uaccess.h>
24#include <linux/regulator/driver.h>
25#include <linux/regulator/machine.h>
26#include <linux/platform_data/lp8755.h>
27
28#define LP8755_REG_BUCK0 0x00
29#define LP8755_REG_BUCK1 0x03
30#define LP8755_REG_BUCK2 0x04
31#define LP8755_REG_BUCK3 0x01
32#define LP8755_REG_BUCK4 0x05
33#define LP8755_REG_BUCK5 0x02
34#define LP8755_REG_MAX 0xFF
35
36#define LP8755_BUCK_EN_M BIT(7)
37#define LP8755_BUCK_LINEAR_OUT_MAX 0x76
38#define LP8755_BUCK_VOUT_M 0x7F
39
Daniel Jeongb59320c2012-12-17 10:24:06 +090040struct lp8755_mphase {
41 int nreg;
42 int buck_num[LP8755_BUCK_MAX];
43};
44
45struct lp8755_chip {
46 struct device *dev;
47 struct regmap *regmap;
48 struct lp8755_platform_data *pdata;
49
50 int irq;
51 unsigned int irqmask;
52
53 int mphase;
54 struct regulator_dev *rdev[LP8755_BUCK_MAX];
55};
56
57/**
58 *lp8755_read : read a single register value from lp8755.
59 *@pchip : device to read from
60 *@reg : register to read from
61 *@val : pointer to store read value
62 */
63static int lp8755_read(struct lp8755_chip *pchip, unsigned int reg,
64 unsigned int *val)
65{
66 return regmap_read(pchip->regmap, reg, val);
67}
68
69/**
70 *lp8755_write : write a single register value to lp8755.
71 *@pchip : device to write to
72 *@reg : register to write to
73 *@val : value to be written
74 */
75static int lp8755_write(struct lp8755_chip *pchip, unsigned int reg,
76 unsigned int val)
77{
78 return regmap_write(pchip->regmap, reg, val);
79}
80
81/**
82 *lp8755_update_bits : set the values of bit fields in lp8755 register.
83 *@pchip : device to read from
84 *@reg : register to update
85 *@mask : bitmask to be changed
86 *@val : value for bitmask
87 */
88static int lp8755_update_bits(struct lp8755_chip *pchip, unsigned int reg,
89 unsigned int mask, unsigned int val)
90{
91 return regmap_update_bits(pchip->regmap, reg, mask, val);
92}
93
94static int lp8755_buck_enable_time(struct regulator_dev *rdev)
95{
96 int ret;
97 unsigned int regval;
98 enum lp8755_bucks id = rdev_get_id(rdev);
99 struct lp8755_chip *pchip = rdev_get_drvdata(rdev);
100
101 ret = lp8755_read(pchip, 0x12 + id, &regval);
102 if (ret < 0) {
103 dev_err(pchip->dev, "i2c acceess error %s\n", __func__);
104 return ret;
105 }
106 return (regval & 0xff) * 100;
107}
108
109static int lp8755_buck_set_mode(struct regulator_dev *rdev, unsigned int mode)
110{
111 int ret;
112 unsigned int regbval = 0x0;
113 enum lp8755_bucks id = rdev_get_id(rdev);
114 struct lp8755_chip *pchip = rdev_get_drvdata(rdev);
115
116 switch (mode) {
117 case REGULATOR_MODE_FAST:
118 /* forced pwm mode */
119 regbval = (0x01 << id);
120 break;
121 case REGULATOR_MODE_NORMAL:
122 /* enable automatic pwm/pfm mode */
123 ret = lp8755_update_bits(pchip, 0x08 + id, 0x20, 0x00);
124 if (ret < 0)
125 goto err_i2c;
126 break;
127 case REGULATOR_MODE_IDLE:
128 /* enable automatic pwm/pfm/lppfm mode */
129 ret = lp8755_update_bits(pchip, 0x08 + id, 0x20, 0x20);
130 if (ret < 0)
131 goto err_i2c;
132
133 ret = lp8755_update_bits(pchip, 0x10, 0x01, 0x01);
134 if (ret < 0)
135 goto err_i2c;
136 break;
137 default:
138 dev_err(pchip->dev, "Not supported buck mode %s\n", __func__);
139 /* forced pwm mode */
140 regbval = (0x01 << id);
141 }
142
143 ret = lp8755_update_bits(pchip, 0x06, 0x01 << id, regbval);
144 if (ret < 0)
145 goto err_i2c;
146 return ret;
147err_i2c:
148 dev_err(pchip->dev, "i2c acceess error %s\n", __func__);
149 return ret;
150}
151
152static unsigned int lp8755_buck_get_mode(struct regulator_dev *rdev)
153{
154 int ret;
155 unsigned int regval;
156 enum lp8755_bucks id = rdev_get_id(rdev);
157 struct lp8755_chip *pchip = rdev_get_drvdata(rdev);
158
159 ret = lp8755_read(pchip, 0x06, &regval);
160 if (ret < 0)
161 goto err_i2c;
162
163 /* mode fast means forced pwm mode */
164 if (regval & (0x01 << id))
165 return REGULATOR_MODE_FAST;
166
167 ret = lp8755_read(pchip, 0x08 + id, &regval);
168 if (ret < 0)
169 goto err_i2c;
170
171 /* mode idle means automatic pwm/pfm/lppfm mode */
172 if (regval & 0x20)
173 return REGULATOR_MODE_IDLE;
174
175 /* mode normal means automatic pwm/pfm mode */
176 return REGULATOR_MODE_NORMAL;
177
178err_i2c:
179 dev_err(pchip->dev, "i2c acceess error %s\n", __func__);
180 return 0;
181}
182
183static int lp8755_buck_set_ramp(struct regulator_dev *rdev, int ramp)
184{
185 int ret;
186 unsigned int regval = 0x00;
187 enum lp8755_bucks id = rdev_get_id(rdev);
188 struct lp8755_chip *pchip = rdev_get_drvdata(rdev);
189
190 /* uV/us */
191 switch (ramp) {
192 case 0 ... 230:
193 regval = 0x07;
194 break;
195 case 231 ... 470:
196 regval = 0x06;
197 break;
198 case 471 ... 940:
199 regval = 0x05;
200 break;
201 case 941 ... 1900:
202 regval = 0x04;
203 break;
204 case 1901 ... 3800:
205 regval = 0x03;
206 break;
207 case 3801 ... 7500:
208 regval = 0x02;
209 break;
210 case 7501 ... 15000:
211 regval = 0x01;
212 break;
213 case 15001 ... 30000:
214 regval = 0x00;
215 break;
216 default:
217 dev_err(pchip->dev,
218 "Not supported ramp value %d %s\n", ramp, __func__);
219 return -EINVAL;
220 }
221
222 ret = lp8755_update_bits(pchip, 0x07 + id, 0x07, regval);
223 if (ret < 0)
224 goto err_i2c;
225 return ret;
226err_i2c:
227 dev_err(pchip->dev, "i2c acceess error %s\n", __func__);
228 return ret;
229}
230
231static struct regulator_ops lp8755_buck_ops = {
Mark Brown56a942e2013-07-02 23:57:35 +0100232 .map_voltage = regulator_map_voltage_linear,
Daniel Jeongb59320c2012-12-17 10:24:06 +0900233 .list_voltage = regulator_list_voltage_linear,
234 .set_voltage_sel = regulator_set_voltage_sel_regmap,
235 .get_voltage_sel = regulator_get_voltage_sel_regmap,
236 .enable = regulator_enable_regmap,
237 .disable = regulator_disable_regmap,
238 .is_enabled = regulator_is_enabled_regmap,
239 .enable_time = lp8755_buck_enable_time,
240 .set_mode = lp8755_buck_set_mode,
241 .get_mode = lp8755_buck_get_mode,
242 .set_ramp_delay = lp8755_buck_set_ramp,
243};
244
245#define lp8755_rail(_id) "lp8755_buck"#_id
246#define lp8755_buck_init(_id)\
247{\
248 .constraints = {\
249 .name = lp8755_rail(_id),\
250 .valid_ops_mask = REGULATOR_CHANGE_VOLTAGE,\
251 .min_uV = 500000,\
252 .max_uV = 1675000,\
253 },\
254}
255
256static struct regulator_init_data lp8755_reg_default[LP8755_BUCK_MAX] = {
Axel Lin510799e2013-01-07 10:28:31 +0800257 [LP8755_BUCK0] = lp8755_buck_init(0),
258 [LP8755_BUCK1] = lp8755_buck_init(1),
259 [LP8755_BUCK2] = lp8755_buck_init(2),
260 [LP8755_BUCK3] = lp8755_buck_init(3),
261 [LP8755_BUCK4] = lp8755_buck_init(4),
262 [LP8755_BUCK5] = lp8755_buck_init(5),
Daniel Jeongb59320c2012-12-17 10:24:06 +0900263};
264
265static const struct lp8755_mphase mphase_buck[MPHASE_CONF_MAX] = {
Axel Lin510799e2013-01-07 10:28:31 +0800266 { 3, { LP8755_BUCK0, LP8755_BUCK3, LP8755_BUCK5 } },
267 { 6, { LP8755_BUCK0, LP8755_BUCK1, LP8755_BUCK2, LP8755_BUCK3,
268 LP8755_BUCK4, LP8755_BUCK5 } },
269 { 5, { LP8755_BUCK0, LP8755_BUCK2, LP8755_BUCK3, LP8755_BUCK4,
270 LP8755_BUCK5} },
271 { 4, { LP8755_BUCK0, LP8755_BUCK3, LP8755_BUCK4, LP8755_BUCK5} },
272 { 3, { LP8755_BUCK0, LP8755_BUCK4, LP8755_BUCK5} },
273 { 2, { LP8755_BUCK0, LP8755_BUCK5} },
274 { 1, { LP8755_BUCK0} },
275 { 2, { LP8755_BUCK0, LP8755_BUCK3} },
276 { 4, { LP8755_BUCK0, LP8755_BUCK2, LP8755_BUCK3, LP8755_BUCK5} },
Daniel Jeongb59320c2012-12-17 10:24:06 +0900277};
278
279static int lp8755_init_data(struct lp8755_chip *pchip)
280{
281 unsigned int regval;
282 int ret, icnt, buck_num;
283 struct lp8755_platform_data *pdata = pchip->pdata;
284
285 /* read back muti-phase configuration */
286 ret = lp8755_read(pchip, 0x3D, &regval);
287 if (ret < 0)
288 goto out_i2c_error;
Axel Lincad877e2012-12-26 11:56:37 +0800289 pchip->mphase = regval & 0x0F;
Daniel Jeongb59320c2012-12-17 10:24:06 +0900290
291 /* set default data based on multi-phase config */
292 for (icnt = 0; icnt < mphase_buck[pchip->mphase].nreg; icnt++) {
293 buck_num = mphase_buck[pchip->mphase].buck_num[icnt];
294 pdata->buck_data[buck_num] = &lp8755_reg_default[buck_num];
295 }
296 return ret;
297
298out_i2c_error:
299 dev_err(pchip->dev, "i2c acceess error %s\n", __func__);
300 return ret;
301}
302
303#define lp8755_buck_desc(_id)\
304{\
305 .name = lp8755_rail(_id),\
306 .id = LP8755_BUCK##_id,\
307 .ops = &lp8755_buck_ops,\
308 .n_voltages = LP8755_BUCK_LINEAR_OUT_MAX+1,\
309 .uV_step = 10000,\
310 .min_uV = 500000,\
311 .type = REGULATOR_VOLTAGE,\
312 .owner = THIS_MODULE,\
313 .enable_reg = LP8755_REG_BUCK##_id,\
314 .enable_mask = LP8755_BUCK_EN_M,\
315 .vsel_reg = LP8755_REG_BUCK##_id,\
316 .vsel_mask = LP8755_BUCK_VOUT_M,\
317}
318
319static struct regulator_desc lp8755_regulators[] = {
320 lp8755_buck_desc(0),
321 lp8755_buck_desc(1),
322 lp8755_buck_desc(2),
323 lp8755_buck_desc(3),
324 lp8755_buck_desc(4),
325 lp8755_buck_desc(5),
326};
327
328static int lp8755_regulator_init(struct lp8755_chip *pchip)
329{
330 int ret, icnt, buck_num;
331 struct lp8755_platform_data *pdata = pchip->pdata;
332 struct regulator_config rconfig = { };
333
334 rconfig.regmap = pchip->regmap;
335 rconfig.dev = pchip->dev;
336 rconfig.driver_data = pchip;
337
338 for (icnt = 0; icnt < mphase_buck[pchip->mphase].nreg; icnt++) {
339 buck_num = mphase_buck[pchip->mphase].buck_num[icnt];
340 rconfig.init_data = pdata->buck_data[buck_num];
341 rconfig.of_node = pchip->dev->of_node;
342 pchip->rdev[buck_num] =
343 regulator_register(&lp8755_regulators[buck_num], &rconfig);
344 if (IS_ERR(pchip->rdev[buck_num])) {
345 ret = PTR_ERR(pchip->rdev[buck_num]);
Axel Lina1a41ab2012-12-25 10:06:20 +0800346 pchip->rdev[buck_num] = NULL;
347 dev_err(pchip->dev, "regulator init failed: buck %d\n",
348 buck_num);
Daniel Jeongb59320c2012-12-17 10:24:06 +0900349 goto err_buck;
350 }
351 }
352
353 return 0;
354
355err_buck:
356 for (icnt = 0; icnt < LP8755_BUCK_MAX; icnt++)
Axel Lina1a41ab2012-12-25 10:06:20 +0800357 regulator_unregister(pchip->rdev[icnt]);
Daniel Jeongb59320c2012-12-17 10:24:06 +0900358 return ret;
359}
360
361static irqreturn_t lp8755_irq_handler(int irq, void *data)
362{
363 int ret, icnt;
364 unsigned int flag0, flag1;
365 struct lp8755_chip *pchip = data;
366
367 /* read flag0 register */
368 ret = lp8755_read(pchip, 0x0D, &flag0);
369 if (ret < 0)
370 goto err_i2c;
371 /* clear flag register to pull up int. pin */
372 ret = lp8755_write(pchip, 0x0D, 0x00);
373 if (ret < 0)
374 goto err_i2c;
375
376 /* sent power fault detection event to specific regulator */
Axel Lin1200c602013-01-26 13:19:47 +0800377 for (icnt = 0; icnt < LP8755_BUCK_MAX; icnt++)
Daniel Jeongb59320c2012-12-17 10:24:06 +0900378 if ((flag0 & (0x4 << icnt))
379 && (pchip->irqmask & (0x04 << icnt))
380 && (pchip->rdev[icnt] != NULL))
381 regulator_notifier_call_chain(pchip->rdev[icnt],
382 LP8755_EVENT_PWR_FAULT,
383 NULL);
384
385 /* read flag1 register */
386 ret = lp8755_read(pchip, 0x0E, &flag1);
387 if (ret < 0)
388 goto err_i2c;
389 /* clear flag register to pull up int. pin */
390 ret = lp8755_write(pchip, 0x0E, 0x00);
391 if (ret < 0)
392 goto err_i2c;
393
394 /* send OCP event to all regualtor devices */
395 if ((flag1 & 0x01) && (pchip->irqmask & 0x01))
396 for (icnt = 0; icnt < LP8755_BUCK_MAX; icnt++)
397 if (pchip->rdev[icnt] != NULL)
398 regulator_notifier_call_chain(pchip->rdev[icnt],
399 LP8755_EVENT_OCP,
400 NULL);
401
402 /* send OVP event to all regualtor devices */
403 if ((flag1 & 0x02) && (pchip->irqmask & 0x02))
404 for (icnt = 0; icnt < LP8755_BUCK_MAX; icnt++)
405 if (pchip->rdev[icnt] != NULL)
406 regulator_notifier_call_chain(pchip->rdev[icnt],
407 LP8755_EVENT_OVP,
408 NULL);
409 return IRQ_HANDLED;
410
411err_i2c:
412 dev_err(pchip->dev, "i2c acceess error %s\n", __func__);
413 return IRQ_NONE;
414}
415
416static int lp8755_int_config(struct lp8755_chip *pchip)
417{
418 int ret;
419 unsigned int regval;
420
421 if (pchip->irq == 0) {
422 dev_warn(pchip->dev, "not use interrupt : %s\n", __func__);
423 return 0;
424 }
425
426 ret = lp8755_read(pchip, 0x0F, &regval);
427 if (ret < 0)
428 goto err_i2c;
429 pchip->irqmask = regval;
430 ret = request_threaded_irq(pchip->irq, NULL, lp8755_irq_handler,
431 IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
432 "lp8755-irq", pchip);
433 if (ret)
434 return ret;
435
436 return ret;
437
438err_i2c:
439 dev_err(pchip->dev, "i2c acceess error %s\n", __func__);
440 return ret;
441}
442
443static const struct regmap_config lp8755_regmap = {
444 .reg_bits = 8,
445 .val_bits = 8,
446 .max_register = LP8755_REG_MAX,
447};
448
449static int lp8755_probe(struct i2c_client *client,
450 const struct i2c_device_id *id)
451{
452 int ret, icnt;
453 struct lp8755_chip *pchip;
454 struct lp8755_platform_data *pdata = client->dev.platform_data;
455
456 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
457 dev_err(&client->dev, "i2c functionality check fail.\n");
458 return -EOPNOTSUPP;
459 }
460
461 pchip = devm_kzalloc(&client->dev,
462 sizeof(struct lp8755_chip), GFP_KERNEL);
463 if (!pchip)
464 return -ENOMEM;
465
466 pchip->dev = &client->dev;
467 pchip->regmap = devm_regmap_init_i2c(client, &lp8755_regmap);
468 if (IS_ERR(pchip->regmap)) {
469 ret = PTR_ERR(pchip->regmap);
470 dev_err(&client->dev, "fail to allocate regmap %d\n", ret);
471 return ret;
472 }
473 i2c_set_clientdata(client, pchip);
474
475 if (pdata != NULL) {
476 pchip->pdata = pdata;
477 pchip->mphase = pdata->mphase;
478 } else {
479 pchip->pdata = devm_kzalloc(pchip->dev,
480 sizeof(struct lp8755_platform_data),
481 GFP_KERNEL);
482 if (!pchip->pdata)
483 return -ENOMEM;
484 ret = lp8755_init_data(pchip);
Axel Lin240a5292013-01-12 14:58:35 +0800485 if (ret < 0) {
486 dev_err(&client->dev, "fail to initialize chip\n");
487 return ret;
488 }
Daniel Jeongb59320c2012-12-17 10:24:06 +0900489 }
490
491 ret = lp8755_regulator_init(pchip);
Axel Lin240a5292013-01-12 14:58:35 +0800492 if (ret < 0) {
493 dev_err(&client->dev, "fail to initialize regulators\n");
Daniel Jeongb59320c2012-12-17 10:24:06 +0900494 goto err_regulator;
Axel Lin240a5292013-01-12 14:58:35 +0800495 }
Daniel Jeongb59320c2012-12-17 10:24:06 +0900496
497 pchip->irq = client->irq;
498 ret = lp8755_int_config(pchip);
Axel Lin240a5292013-01-12 14:58:35 +0800499 if (ret < 0) {
500 dev_err(&client->dev, "fail to irq config\n");
Daniel Jeongb59320c2012-12-17 10:24:06 +0900501 goto err_irq;
Axel Lin240a5292013-01-12 14:58:35 +0800502 }
Daniel Jeongb59320c2012-12-17 10:24:06 +0900503
504 return ret;
505
506err_irq:
Daniel Jeongb59320c2012-12-17 10:24:06 +0900507 for (icnt = 0; icnt < mphase_buck[pchip->mphase].nreg; icnt++)
508 regulator_unregister(pchip->rdev[icnt]);
509
510err_regulator:
Daniel Jeongb59320c2012-12-17 10:24:06 +0900511 /* output disable */
Axel Lin1200c602013-01-26 13:19:47 +0800512 for (icnt = 0; icnt < LP8755_BUCK_MAX; icnt++)
Daniel Jeongb59320c2012-12-17 10:24:06 +0900513 lp8755_write(pchip, icnt, 0x00);
514
Daniel Jeongb59320c2012-12-17 10:24:06 +0900515 return ret;
516}
517
518static int lp8755_remove(struct i2c_client *client)
519{
520 int icnt;
521 struct lp8755_chip *pchip = i2c_get_clientdata(client);
522
523 for (icnt = 0; icnt < mphase_buck[pchip->mphase].nreg; icnt++)
524 regulator_unregister(pchip->rdev[icnt]);
525
Axel Lin1200c602013-01-26 13:19:47 +0800526 for (icnt = 0; icnt < LP8755_BUCK_MAX; icnt++)
Daniel Jeongb59320c2012-12-17 10:24:06 +0900527 lp8755_write(pchip, icnt, 0x00);
528
529 if (pchip->irq != 0)
530 free_irq(pchip->irq, pchip);
531
532 return 0;
533}
534
535static const struct i2c_device_id lp8755_id[] = {
536 {LP8755_NAME, 0},
537 {}
538};
539
540MODULE_DEVICE_TABLE(i2c, lp8755_id);
541
542static struct i2c_driver lp8755_i2c_driver = {
543 .driver = {
544 .name = LP8755_NAME,
545 },
546 .probe = lp8755_probe,
Axel Lina1a41ab2012-12-25 10:06:20 +0800547 .remove = lp8755_remove,
Daniel Jeongb59320c2012-12-17 10:24:06 +0900548 .id_table = lp8755_id,
549};
550
551static int __init lp8755_init(void)
552{
553 return i2c_add_driver(&lp8755_i2c_driver);
554}
555
556subsys_initcall(lp8755_init);
557
558static void __exit lp8755_exit(void)
559{
560 i2c_del_driver(&lp8755_i2c_driver);
561}
562
563module_exit(lp8755_exit);
564
565MODULE_DESCRIPTION("Texas Instruments lp8755 driver");
566MODULE_AUTHOR("Daniel Jeong <daniel.jeong@ti.com>");
567MODULE_LICENSE("GPL v2");