scsi: qla2xxx: Fix recursive mailbox timeout
[linux/fpc-iii.git] / drivers / regulator / ltc3589.c
blob18d5b01ddcb20dc3f8929db2c6adda3344985735
1 /*
2 * Linear Technology LTC3589,LTC3589-1 regulator support
4 * Copyright (c) 2014 Philipp Zabel <p.zabel@pengutronix.de>, Pengutronix
6 * See file CREDITS for list of people who contributed to this
7 * project.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2
11 * as published by the Free Software Foundation.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
19 #include <linux/i2c.h>
20 #include <linux/init.h>
21 #include <linux/interrupt.h>
22 #include <linux/module.h>
23 #include <linux/kernel.h>
24 #include <linux/of.h>
25 #include <linux/of_device.h>
26 #include <linux/regmap.h>
27 #include <linux/regulator/driver.h>
28 #include <linux/regulator/of_regulator.h>
30 #define DRIVER_NAME "ltc3589"
32 #define LTC3589_IRQSTAT 0x02
33 #define LTC3589_SCR1 0x07
34 #define LTC3589_OVEN 0x10
35 #define LTC3589_SCR2 0x12
36 #define LTC3589_PGSTAT 0x13
37 #define LTC3589_VCCR 0x20
38 #define LTC3589_CLIRQ 0x21
39 #define LTC3589_B1DTV1 0x23
40 #define LTC3589_B1DTV2 0x24
41 #define LTC3589_VRRCR 0x25
42 #define LTC3589_B2DTV1 0x26
43 #define LTC3589_B2DTV2 0x27
44 #define LTC3589_B3DTV1 0x29
45 #define LTC3589_B3DTV2 0x2a
46 #define LTC3589_L2DTV1 0x32
47 #define LTC3589_L2DTV2 0x33
49 #define LTC3589_IRQSTAT_PGOOD_TIMEOUT BIT(3)
50 #define LTC3589_IRQSTAT_UNDERVOLT_WARN BIT(4)
51 #define LTC3589_IRQSTAT_UNDERVOLT_FAULT BIT(5)
52 #define LTC3589_IRQSTAT_THERMAL_WARN BIT(6)
53 #define LTC3589_IRQSTAT_THERMAL_FAULT BIT(7)
55 #define LTC3589_OVEN_SW1 BIT(0)
56 #define LTC3589_OVEN_SW2 BIT(1)
57 #define LTC3589_OVEN_SW3 BIT(2)
58 #define LTC3589_OVEN_BB_OUT BIT(3)
59 #define LTC3589_OVEN_LDO2 BIT(4)
60 #define LTC3589_OVEN_LDO3 BIT(5)
61 #define LTC3589_OVEN_LDO4 BIT(6)
62 #define LTC3589_OVEN_SW_CTRL BIT(7)
64 #define LTC3589_VCCR_SW1_GO BIT(0)
65 #define LTC3589_VCCR_SW2_GO BIT(2)
66 #define LTC3589_VCCR_SW3_GO BIT(4)
67 #define LTC3589_VCCR_LDO2_GO BIT(6)
69 enum ltc3589_variant {
70 LTC3589,
71 LTC3589_1,
72 LTC3589_2,
75 enum ltc3589_reg {
76 LTC3589_SW1,
77 LTC3589_SW2,
78 LTC3589_SW3,
79 LTC3589_BB_OUT,
80 LTC3589_LDO1,
81 LTC3589_LDO2,
82 LTC3589_LDO3,
83 LTC3589_LDO4,
84 LTC3589_NUM_REGULATORS,
87 struct ltc3589_regulator {
88 struct regulator_desc desc;
90 /* External feedback voltage divider */
91 unsigned int r1;
92 unsigned int r2;
95 struct ltc3589 {
96 struct regmap *regmap;
97 struct device *dev;
98 enum ltc3589_variant variant;
99 struct ltc3589_regulator regulator_descs[LTC3589_NUM_REGULATORS];
100 struct regulator_dev *regulators[LTC3589_NUM_REGULATORS];
103 static const int ltc3589_ldo4[] = {
104 2800000, 2500000, 1800000, 3300000,
107 static const int ltc3589_12_ldo4[] = {
108 1200000, 1800000, 2500000, 3200000,
111 static int ltc3589_set_ramp_delay(struct regulator_dev *rdev, int ramp_delay)
113 struct ltc3589 *ltc3589 = rdev_get_drvdata(rdev);
114 int sel, shift;
116 if (unlikely(ramp_delay <= 0))
117 return -EINVAL;
119 /* VRRCR slew rate offsets are the same as VCCR go bit offsets */
120 shift = ffs(rdev->desc->apply_bit) - 1;
122 /* The slew rate can be set to 0.88, 1.75, 3.5, or 7 mV/uS */
123 for (sel = 0; sel < 4; sel++) {
124 if ((880 << sel) >= ramp_delay) {
125 return regmap_update_bits(ltc3589->regmap,
126 LTC3589_VRRCR,
127 0x3 << shift, sel << shift);
130 return -EINVAL;
133 static int ltc3589_set_suspend_voltage(struct regulator_dev *rdev, int uV)
135 struct ltc3589 *ltc3589 = rdev_get_drvdata(rdev);
136 int sel;
138 sel = regulator_map_voltage_linear(rdev, uV, uV);
139 if (sel < 0)
140 return sel;
142 /* DTV2 register follows right after the corresponding DTV1 register */
143 return regmap_update_bits(ltc3589->regmap, rdev->desc->vsel_reg + 1,
144 rdev->desc->vsel_mask, sel);
147 static int ltc3589_set_suspend_mode(struct regulator_dev *rdev,
148 unsigned int mode)
150 struct ltc3589 *ltc3589 = rdev_get_drvdata(rdev);
151 int mask, bit = 0;
153 /* VCCR reference selects are right next to the VCCR go bits */
154 mask = rdev->desc->apply_bit << 1;
156 if (mode == REGULATOR_MODE_STANDBY)
157 bit = mask; /* Select DTV2 */
159 mask |= rdev->desc->apply_bit;
160 bit |= rdev->desc->apply_bit;
161 return regmap_update_bits(ltc3589->regmap, LTC3589_VCCR, mask, bit);
164 /* SW1, SW2, SW3, LDO2 */
165 static const struct regulator_ops ltc3589_linear_regulator_ops = {
166 .enable = regulator_enable_regmap,
167 .disable = regulator_disable_regmap,
168 .is_enabled = regulator_is_enabled_regmap,
169 .list_voltage = regulator_list_voltage_linear,
170 .set_voltage_sel = regulator_set_voltage_sel_regmap,
171 .get_voltage_sel = regulator_get_voltage_sel_regmap,
172 .set_ramp_delay = ltc3589_set_ramp_delay,
173 .set_voltage_time_sel = regulator_set_voltage_time_sel,
174 .set_suspend_voltage = ltc3589_set_suspend_voltage,
175 .set_suspend_mode = ltc3589_set_suspend_mode,
178 /* BB_OUT, LDO3 */
179 static const struct regulator_ops ltc3589_fixed_regulator_ops = {
180 .enable = regulator_enable_regmap,
181 .disable = regulator_disable_regmap,
182 .is_enabled = regulator_is_enabled_regmap,
185 /* LDO1 */
186 static const struct regulator_ops ltc3589_fixed_standby_regulator_ops = {
189 /* LDO4 */
190 static const struct regulator_ops ltc3589_table_regulator_ops = {
191 .enable = regulator_enable_regmap,
192 .disable = regulator_disable_regmap,
193 .is_enabled = regulator_is_enabled_regmap,
194 .list_voltage = regulator_list_voltage_table,
195 .set_voltage_sel = regulator_set_voltage_sel_regmap,
196 .get_voltage_sel = regulator_get_voltage_sel_regmap,
200 #define LTC3589_REG(_name, _ops, en_bit, dtv1_reg, dtv_mask, go_bit) \
201 [LTC3589_ ## _name] = { \
202 .desc = { \
203 .name = #_name, \
204 .n_voltages = (dtv_mask) + 1, \
205 .min_uV = (go_bit) ? 362500 : 0, \
206 .uV_step = (go_bit) ? 12500 : 0, \
207 .ramp_delay = (go_bit) ? 1750 : 0, \
208 .fixed_uV = (dtv_mask) ? 0 : 800000, \
209 .ops = &ltc3589_ ## _ops ## _regulator_ops, \
210 .type = REGULATOR_VOLTAGE, \
211 .id = LTC3589_ ## _name, \
212 .owner = THIS_MODULE, \
213 .vsel_reg = (dtv1_reg), \
214 .vsel_mask = (dtv_mask), \
215 .apply_reg = (go_bit) ? LTC3589_VCCR : 0, \
216 .apply_bit = (go_bit), \
217 .enable_reg = (en_bit) ? LTC3589_OVEN : 0, \
218 .enable_mask = (en_bit), \
219 }, \
222 #define LTC3589_LINEAR_REG(_name, _dtv1) \
223 LTC3589_REG(_name, linear, LTC3589_OVEN_ ## _name, \
224 LTC3589_ ## _dtv1, 0x1f, \
225 LTC3589_VCCR_ ## _name ## _GO)
227 #define LTC3589_FIXED_REG(_name) \
228 LTC3589_REG(_name, fixed, LTC3589_OVEN_ ## _name, 0, 0, 0)
230 static struct ltc3589_regulator ltc3589_regulators[LTC3589_NUM_REGULATORS] = {
231 LTC3589_LINEAR_REG(SW1, B1DTV1),
232 LTC3589_LINEAR_REG(SW2, B2DTV1),
233 LTC3589_LINEAR_REG(SW3, B3DTV1),
234 LTC3589_FIXED_REG(BB_OUT),
235 LTC3589_REG(LDO1, fixed_standby, 0, 0, 0, 0),
236 LTC3589_LINEAR_REG(LDO2, L2DTV1),
237 LTC3589_FIXED_REG(LDO3),
238 LTC3589_REG(LDO4, table, LTC3589_OVEN_LDO4, LTC3589_L2DTV2, 0x60, 0),
241 #ifdef CONFIG_OF
242 static struct of_regulator_match ltc3589_matches[LTC3589_NUM_REGULATORS] = {
243 { .name = "sw1", },
244 { .name = "sw2", },
245 { .name = "sw3", },
246 { .name = "bb-out", },
247 { .name = "ldo1", }, /* standby */
248 { .name = "ldo2", },
249 { .name = "ldo3", },
250 { .name = "ldo4", },
253 static int ltc3589_parse_regulators_dt(struct ltc3589 *ltc3589)
255 struct device *dev = ltc3589->dev;
256 struct device_node *node;
257 int i, ret;
259 node = of_get_child_by_name(dev->of_node, "regulators");
260 if (!node) {
261 dev_err(dev, "regulators node not found\n");
262 return -EINVAL;
265 ret = of_regulator_match(dev, node, ltc3589_matches,
266 ARRAY_SIZE(ltc3589_matches));
267 of_node_put(node);
268 if (ret < 0) {
269 dev_err(dev, "Error parsing regulator init data: %d\n", ret);
270 return ret;
272 if (ret != LTC3589_NUM_REGULATORS) {
273 dev_err(dev, "Only %d regulators described in device tree\n",
274 ret);
275 return -EINVAL;
278 /* Parse feedback voltage dividers. LDO3 and LDO4 don't have them */
279 for (i = 0; i < LTC3589_LDO3; i++) {
280 struct ltc3589_regulator *desc = &ltc3589->regulator_descs[i];
281 struct device_node *np = ltc3589_matches[i].of_node;
282 u32 vdiv[2];
284 ret = of_property_read_u32_array(np, "lltc,fb-voltage-divider",
285 vdiv, 2);
286 if (ret) {
287 dev_err(dev, "Failed to parse voltage divider: %d\n",
288 ret);
289 return ret;
292 desc->r1 = vdiv[0];
293 desc->r2 = vdiv[1];
296 return 0;
299 static inline struct regulator_init_data *match_init_data(int index)
301 return ltc3589_matches[index].init_data;
304 static inline struct device_node *match_of_node(int index)
306 return ltc3589_matches[index].of_node;
308 #else
309 static inline int ltc3589_parse_regulators_dt(struct ltc3589 *ltc3589)
311 return 0;
314 static inline struct regulator_init_data *match_init_data(int index)
316 return NULL;
319 static inline struct device_node *match_of_node(int index)
321 return NULL;
323 #endif
325 static bool ltc3589_writeable_reg(struct device *dev, unsigned int reg)
327 switch (reg) {
328 case LTC3589_IRQSTAT:
329 case LTC3589_SCR1:
330 case LTC3589_OVEN:
331 case LTC3589_SCR2:
332 case LTC3589_VCCR:
333 case LTC3589_CLIRQ:
334 case LTC3589_B1DTV1:
335 case LTC3589_B1DTV2:
336 case LTC3589_VRRCR:
337 case LTC3589_B2DTV1:
338 case LTC3589_B2DTV2:
339 case LTC3589_B3DTV1:
340 case LTC3589_B3DTV2:
341 case LTC3589_L2DTV1:
342 case LTC3589_L2DTV2:
343 return true;
345 return false;
348 static bool ltc3589_readable_reg(struct device *dev, unsigned int reg)
350 switch (reg) {
351 case LTC3589_IRQSTAT:
352 case LTC3589_SCR1:
353 case LTC3589_OVEN:
354 case LTC3589_SCR2:
355 case LTC3589_PGSTAT:
356 case LTC3589_VCCR:
357 case LTC3589_B1DTV1:
358 case LTC3589_B1DTV2:
359 case LTC3589_VRRCR:
360 case LTC3589_B2DTV1:
361 case LTC3589_B2DTV2:
362 case LTC3589_B3DTV1:
363 case LTC3589_B3DTV2:
364 case LTC3589_L2DTV1:
365 case LTC3589_L2DTV2:
366 return true;
368 return false;
371 static bool ltc3589_volatile_reg(struct device *dev, unsigned int reg)
373 switch (reg) {
374 case LTC3589_IRQSTAT:
375 case LTC3589_PGSTAT:
376 case LTC3589_VCCR:
377 return true;
379 return false;
382 static const struct reg_default ltc3589_reg_defaults[] = {
383 { LTC3589_SCR1, 0x00 },
384 { LTC3589_OVEN, 0x00 },
385 { LTC3589_SCR2, 0x00 },
386 { LTC3589_VCCR, 0x00 },
387 { LTC3589_B1DTV1, 0x19 },
388 { LTC3589_B1DTV2, 0x19 },
389 { LTC3589_VRRCR, 0xff },
390 { LTC3589_B2DTV1, 0x19 },
391 { LTC3589_B2DTV2, 0x19 },
392 { LTC3589_B3DTV1, 0x19 },
393 { LTC3589_B3DTV2, 0x19 },
394 { LTC3589_L2DTV1, 0x19 },
395 { LTC3589_L2DTV2, 0x19 },
398 static const struct regmap_config ltc3589_regmap_config = {
399 .reg_bits = 8,
400 .val_bits = 8,
401 .writeable_reg = ltc3589_writeable_reg,
402 .readable_reg = ltc3589_readable_reg,
403 .volatile_reg = ltc3589_volatile_reg,
404 .max_register = LTC3589_L2DTV2,
405 .reg_defaults = ltc3589_reg_defaults,
406 .num_reg_defaults = ARRAY_SIZE(ltc3589_reg_defaults),
407 .use_single_rw = true,
408 .cache_type = REGCACHE_RBTREE,
412 static irqreturn_t ltc3589_isr(int irq, void *dev_id)
414 struct ltc3589 *ltc3589 = dev_id;
415 unsigned int i, irqstat, event;
417 regmap_read(ltc3589->regmap, LTC3589_IRQSTAT, &irqstat);
419 if (irqstat & LTC3589_IRQSTAT_THERMAL_WARN) {
420 event = REGULATOR_EVENT_OVER_TEMP;
421 for (i = 0; i < LTC3589_NUM_REGULATORS; i++)
422 regulator_notifier_call_chain(ltc3589->regulators[i],
423 event, NULL);
426 if (irqstat & LTC3589_IRQSTAT_UNDERVOLT_WARN) {
427 event = REGULATOR_EVENT_UNDER_VOLTAGE;
428 for (i = 0; i < LTC3589_NUM_REGULATORS; i++)
429 regulator_notifier_call_chain(ltc3589->regulators[i],
430 event, NULL);
433 /* Clear warning condition */
434 regmap_write(ltc3589->regmap, LTC3589_CLIRQ, 0);
436 return IRQ_HANDLED;
439 static inline unsigned int ltc3589_scale(unsigned int uV, u32 r1, u32 r2)
441 uint64_t tmp;
442 if (uV == 0)
443 return 0;
444 tmp = (uint64_t)uV * r1;
445 do_div(tmp, r2);
446 return uV + (unsigned int)tmp;
449 static void ltc3589_apply_fb_voltage_divider(struct ltc3589_regulator *rdesc)
451 struct regulator_desc *desc = &rdesc->desc;
453 if (!rdesc->r1 || !rdesc->r2)
454 return;
456 desc->min_uV = ltc3589_scale(desc->min_uV, rdesc->r1, rdesc->r2);
457 desc->uV_step = ltc3589_scale(desc->uV_step, rdesc->r1, rdesc->r2);
458 desc->fixed_uV = ltc3589_scale(desc->fixed_uV, rdesc->r1, rdesc->r2);
461 static int ltc3589_probe(struct i2c_client *client,
462 const struct i2c_device_id *id)
464 struct device *dev = &client->dev;
465 struct ltc3589_regulator *descs;
466 struct ltc3589 *ltc3589;
467 int i, ret;
469 ltc3589 = devm_kzalloc(dev, sizeof(*ltc3589), GFP_KERNEL);
470 if (!ltc3589)
471 return -ENOMEM;
473 i2c_set_clientdata(client, ltc3589);
474 if (client->dev.of_node)
475 ltc3589->variant = (enum ltc3589_variant)
476 of_device_get_match_data(&client->dev);
477 else
478 ltc3589->variant = id->driver_data;
479 ltc3589->dev = dev;
481 descs = ltc3589->regulator_descs;
482 memcpy(descs, ltc3589_regulators, sizeof(ltc3589_regulators));
483 if (ltc3589->variant == LTC3589) {
484 descs[LTC3589_LDO3].desc.fixed_uV = 1800000;
485 descs[LTC3589_LDO4].desc.volt_table = ltc3589_ldo4;
486 } else {
487 descs[LTC3589_LDO3].desc.fixed_uV = 2800000;
488 descs[LTC3589_LDO4].desc.volt_table = ltc3589_12_ldo4;
491 ltc3589->regmap = devm_regmap_init_i2c(client, &ltc3589_regmap_config);
492 if (IS_ERR(ltc3589->regmap)) {
493 ret = PTR_ERR(ltc3589->regmap);
494 dev_err(dev, "failed to initialize regmap: %d\n", ret);
495 return ret;
498 ret = ltc3589_parse_regulators_dt(ltc3589);
499 if (ret)
500 return ret;
502 for (i = 0; i < LTC3589_NUM_REGULATORS; i++) {
503 struct ltc3589_regulator *rdesc = &ltc3589->regulator_descs[i];
504 struct regulator_desc *desc = &rdesc->desc;
505 struct regulator_init_data *init_data;
506 struct regulator_config config = { };
508 init_data = match_init_data(i);
510 if (i < LTC3589_LDO3)
511 ltc3589_apply_fb_voltage_divider(rdesc);
513 config.dev = dev;
514 config.init_data = init_data;
515 config.driver_data = ltc3589;
516 config.of_node = match_of_node(i);
518 ltc3589->regulators[i] = devm_regulator_register(dev, desc,
519 &config);
520 if (IS_ERR(ltc3589->regulators[i])) {
521 ret = PTR_ERR(ltc3589->regulators[i]);
522 dev_err(dev, "failed to register regulator %s: %d\n",
523 desc->name, ret);
524 return ret;
528 if (client->irq) {
529 ret = devm_request_threaded_irq(dev, client->irq, NULL,
530 ltc3589_isr,
531 IRQF_TRIGGER_LOW | IRQF_ONESHOT,
532 client->name, ltc3589);
533 if (ret) {
534 dev_err(dev, "Failed to request IRQ: %d\n", ret);
535 return ret;
539 return 0;
542 static const struct i2c_device_id ltc3589_i2c_id[] = {
543 { "ltc3589", LTC3589 },
544 { "ltc3589-1", LTC3589_1 },
545 { "ltc3589-2", LTC3589_2 },
548 MODULE_DEVICE_TABLE(i2c, ltc3589_i2c_id);
550 static const struct of_device_id ltc3589_of_match[] = {
552 .compatible = "lltc,ltc3589",
553 .data = (void *)LTC3589,
556 .compatible = "lltc,ltc3589-1",
557 .data = (void *)LTC3589_1,
560 .compatible = "lltc,ltc3589-2",
561 .data = (void *)LTC3589_2,
563 { },
565 MODULE_DEVICE_TABLE(of, ltc3589_of_match);
567 static struct i2c_driver ltc3589_driver = {
568 .driver = {
569 .name = DRIVER_NAME,
570 .of_match_table = of_match_ptr(ltc3589_of_match),
572 .probe = ltc3589_probe,
573 .id_table = ltc3589_i2c_id,
575 module_i2c_driver(ltc3589_driver);
577 MODULE_AUTHOR("Philipp Zabel <p.zabel@pengutronix.de>");
578 MODULE_DESCRIPTION("Regulator driver for Linear Technology LTC3589(-1,2)");
579 MODULE_LICENSE("GPL v2");