ltc3589.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557
  1. /*
  2. * Linear Technology LTC3589,LTC3589-1 regulator support
  3. *
  4. * Copyright (c) 2014 Philipp Zabel <p.zabel@pengutronix.de>, Pengutronix
  5. *
  6. * See file CREDITS for list of people who contributed to this
  7. * project.
  8. *
  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.
  12. *
  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.
  17. *
  18. */
  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/regmap.h>
  26. #include <linux/regulator/driver.h>
  27. #include <linux/regulator/of_regulator.h>
  28. #define DRIVER_NAME "ltc3589"
  29. #define LTC3589_IRQSTAT 0x02
  30. #define LTC3589_SCR1 0x07
  31. #define LTC3589_OVEN 0x10
  32. #define LTC3589_SCR2 0x12
  33. #define LTC3589_PGSTAT 0x13
  34. #define LTC3589_VCCR 0x20
  35. #define LTC3589_CLIRQ 0x21
  36. #define LTC3589_B1DTV1 0x23
  37. #define LTC3589_B1DTV2 0x24
  38. #define LTC3589_VRRCR 0x25
  39. #define LTC3589_B2DTV1 0x26
  40. #define LTC3589_B2DTV2 0x27
  41. #define LTC3589_B3DTV1 0x29
  42. #define LTC3589_B3DTV2 0x2a
  43. #define LTC3589_L2DTV1 0x32
  44. #define LTC3589_L2DTV2 0x33
  45. #define LTC3589_IRQSTAT_PGOOD_TIMEOUT BIT(3)
  46. #define LTC3589_IRQSTAT_UNDERVOLT_WARN BIT(4)
  47. #define LTC3589_IRQSTAT_UNDERVOLT_FAULT BIT(5)
  48. #define LTC3589_IRQSTAT_THERMAL_WARN BIT(6)
  49. #define LTC3589_IRQSTAT_THERMAL_FAULT BIT(7)
  50. #define LTC3589_OVEN_SW1 BIT(0)
  51. #define LTC3589_OVEN_SW2 BIT(1)
  52. #define LTC3589_OVEN_SW3 BIT(2)
  53. #define LTC3589_OVEN_BB_OUT BIT(3)
  54. #define LTC3589_OVEN_LDO2 BIT(4)
  55. #define LTC3589_OVEN_LDO3 BIT(5)
  56. #define LTC3589_OVEN_LDO4 BIT(6)
  57. #define LTC3589_OVEN_SW_CTRL BIT(7)
  58. #define LTC3589_VCCR_SW1_GO BIT(0)
  59. #define LTC3589_VCCR_SW2_GO BIT(2)
  60. #define LTC3589_VCCR_SW3_GO BIT(4)
  61. #define LTC3589_VCCR_LDO2_GO BIT(6)
  62. enum ltc3589_variant {
  63. LTC3589,
  64. LTC3589_1,
  65. LTC3589_2,
  66. };
  67. enum ltc3589_reg {
  68. LTC3589_SW1,
  69. LTC3589_SW2,
  70. LTC3589_SW3,
  71. LTC3589_BB_OUT,
  72. LTC3589_LDO1,
  73. LTC3589_LDO2,
  74. LTC3589_LDO3,
  75. LTC3589_LDO4,
  76. LTC3589_NUM_REGULATORS,
  77. };
  78. struct ltc3589_regulator {
  79. struct regulator_desc desc;
  80. /* External feedback voltage divider */
  81. unsigned int r1;
  82. unsigned int r2;
  83. };
  84. struct ltc3589 {
  85. struct regmap *regmap;
  86. struct device *dev;
  87. enum ltc3589_variant variant;
  88. struct ltc3589_regulator regulator_descs[LTC3589_NUM_REGULATORS];
  89. struct regulator_dev *regulators[LTC3589_NUM_REGULATORS];
  90. };
  91. static const int ltc3589_ldo4[] = {
  92. 2800000, 2500000, 1800000, 3300000,
  93. };
  94. static const int ltc3589_12_ldo4[] = {
  95. 1200000, 1800000, 2500000, 3200000,
  96. };
  97. static int ltc3589_set_ramp_delay(struct regulator_dev *rdev, int ramp_delay)
  98. {
  99. struct ltc3589 *ltc3589 = rdev_get_drvdata(rdev);
  100. int sel, shift;
  101. if (unlikely(ramp_delay <= 0))
  102. return -EINVAL;
  103. /* VRRCR slew rate offsets are the same as VCCR go bit offsets */
  104. shift = ffs(rdev->desc->apply_bit) - 1;
  105. /* The slew rate can be set to 0.88, 1.75, 3.5, or 7 mV/uS */
  106. for (sel = 0; sel < 4; sel++) {
  107. if ((880 << sel) >= ramp_delay) {
  108. return regmap_update_bits(ltc3589->regmap,
  109. LTC3589_VRRCR,
  110. 0x3 << shift, sel << shift);
  111. }
  112. }
  113. return -EINVAL;
  114. }
  115. static int ltc3589_set_suspend_voltage(struct regulator_dev *rdev, int uV)
  116. {
  117. struct ltc3589 *ltc3589 = rdev_get_drvdata(rdev);
  118. int sel;
  119. sel = regulator_map_voltage_linear(rdev, uV, uV);
  120. if (sel < 0)
  121. return sel;
  122. /* DTV2 register follows right after the corresponding DTV1 register */
  123. return regmap_update_bits(ltc3589->regmap, rdev->desc->vsel_reg + 1,
  124. rdev->desc->vsel_mask, sel);
  125. }
  126. static int ltc3589_set_suspend_mode(struct regulator_dev *rdev,
  127. unsigned int mode)
  128. {
  129. struct ltc3589 *ltc3589 = rdev_get_drvdata(rdev);
  130. int mask, bit = 0;
  131. /* VCCR reference selects are right next to the VCCR go bits */
  132. mask = rdev->desc->apply_bit << 1;
  133. if (mode == REGULATOR_MODE_STANDBY)
  134. bit = mask; /* Select DTV2 */
  135. mask |= rdev->desc->apply_bit;
  136. bit |= rdev->desc->apply_bit;
  137. return regmap_update_bits(ltc3589->regmap, LTC3589_VCCR, mask, bit);
  138. }
  139. /* SW1, SW2, SW3, LDO2 */
  140. static struct regulator_ops ltc3589_linear_regulator_ops = {
  141. .enable = regulator_enable_regmap,
  142. .disable = regulator_disable_regmap,
  143. .is_enabled = regulator_is_enabled_regmap,
  144. .list_voltage = regulator_list_voltage_linear,
  145. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  146. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  147. .set_ramp_delay = ltc3589_set_ramp_delay,
  148. .set_voltage_time_sel = regulator_set_voltage_time_sel,
  149. .set_suspend_voltage = ltc3589_set_suspend_voltage,
  150. .set_suspend_mode = ltc3589_set_suspend_mode,
  151. };
  152. /* BB_OUT, LDO3 */
  153. static struct regulator_ops ltc3589_fixed_regulator_ops = {
  154. .enable = regulator_enable_regmap,
  155. .disable = regulator_disable_regmap,
  156. .is_enabled = regulator_is_enabled_regmap,
  157. };
  158. /* LDO1 */
  159. static struct regulator_ops ltc3589_fixed_standby_regulator_ops = {
  160. };
  161. /* LDO4 */
  162. static struct regulator_ops ltc3589_table_regulator_ops = {
  163. .enable = regulator_enable_regmap,
  164. .disable = regulator_disable_regmap,
  165. .is_enabled = regulator_is_enabled_regmap,
  166. .list_voltage = regulator_list_voltage_table,
  167. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  168. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  169. };
  170. #define LTC3589_REG(_name, _ops, en_bit, dtv1_reg, dtv_mask, go_bit) \
  171. [LTC3589_ ## _name] = { \
  172. .desc = { \
  173. .name = #_name, \
  174. .n_voltages = (dtv_mask) + 1, \
  175. .min_uV = (go_bit) ? 362500 : 0, \
  176. .uV_step = (go_bit) ? 12500 : 0, \
  177. .ramp_delay = (go_bit) ? 1750 : 0, \
  178. .fixed_uV = (dtv_mask) ? 0 : 800000, \
  179. .ops = &ltc3589_ ## _ops ## _regulator_ops, \
  180. .type = REGULATOR_VOLTAGE, \
  181. .id = LTC3589_ ## _name, \
  182. .owner = THIS_MODULE, \
  183. .vsel_reg = (dtv1_reg), \
  184. .vsel_mask = (dtv_mask), \
  185. .apply_reg = (go_bit) ? LTC3589_VCCR : 0, \
  186. .apply_bit = (go_bit), \
  187. .enable_reg = (en_bit) ? LTC3589_OVEN : 0, \
  188. .enable_mask = (en_bit), \
  189. }, \
  190. }
  191. #define LTC3589_LINEAR_REG(_name, _dtv1) \
  192. LTC3589_REG(_name, linear, LTC3589_OVEN_ ## _name, \
  193. LTC3589_ ## _dtv1, 0x1f, \
  194. LTC3589_VCCR_ ## _name ## _GO)
  195. #define LTC3589_FIXED_REG(_name) \
  196. LTC3589_REG(_name, fixed, LTC3589_OVEN_ ## _name, 0, 0, 0)
  197. static struct ltc3589_regulator ltc3589_regulators[LTC3589_NUM_REGULATORS] = {
  198. LTC3589_LINEAR_REG(SW1, B1DTV1),
  199. LTC3589_LINEAR_REG(SW2, B2DTV1),
  200. LTC3589_LINEAR_REG(SW3, B3DTV1),
  201. LTC3589_FIXED_REG(BB_OUT),
  202. LTC3589_REG(LDO1, fixed_standby, 0, 0, 0, 0),
  203. LTC3589_LINEAR_REG(LDO2, L2DTV1),
  204. LTC3589_FIXED_REG(LDO3),
  205. LTC3589_REG(LDO4, table, LTC3589_OVEN_LDO4, LTC3589_L2DTV2, 0x60, 0),
  206. };
  207. #ifdef CONFIG_OF
  208. static struct of_regulator_match ltc3589_matches[LTC3589_NUM_REGULATORS] = {
  209. { .name = "sw1", },
  210. { .name = "sw2", },
  211. { .name = "sw3", },
  212. { .name = "bb-out", },
  213. { .name = "ldo1", }, /* standby */
  214. { .name = "ldo2", },
  215. { .name = "ldo3", },
  216. { .name = "ldo4", },
  217. };
  218. static int ltc3589_parse_regulators_dt(struct ltc3589 *ltc3589)
  219. {
  220. struct device *dev = ltc3589->dev;
  221. struct device_node *node;
  222. int i, ret;
  223. node = of_get_child_by_name(dev->of_node, "regulators");
  224. if (!node) {
  225. dev_err(dev, "regulators node not found\n");
  226. return -EINVAL;
  227. }
  228. ret = of_regulator_match(dev, node, ltc3589_matches,
  229. ARRAY_SIZE(ltc3589_matches));
  230. of_node_put(node);
  231. if (ret < 0) {
  232. dev_err(dev, "Error parsing regulator init data: %d\n", ret);
  233. return ret;
  234. }
  235. if (ret != LTC3589_NUM_REGULATORS) {
  236. dev_err(dev, "Only %d regulators described in device tree\n",
  237. ret);
  238. return -EINVAL;
  239. }
  240. /* Parse feedback voltage dividers. LDO3 and LDO4 don't have them */
  241. for (i = 0; i < LTC3589_LDO3; i++) {
  242. struct ltc3589_regulator *desc = &ltc3589->regulator_descs[i];
  243. struct device_node *np = ltc3589_matches[i].of_node;
  244. u32 vdiv[2];
  245. ret = of_property_read_u32_array(np, "lltc,fb-voltage-divider",
  246. vdiv, 2);
  247. if (ret) {
  248. dev_err(dev, "Failed to parse voltage divider: %d\n",
  249. ret);
  250. return ret;
  251. }
  252. desc->r1 = vdiv[0];
  253. desc->r2 = vdiv[1];
  254. }
  255. return 0;
  256. }
  257. static inline struct regulator_init_data *match_init_data(int index)
  258. {
  259. return ltc3589_matches[index].init_data;
  260. }
  261. static inline struct device_node *match_of_node(int index)
  262. {
  263. return ltc3589_matches[index].of_node;
  264. }
  265. #else
  266. static inline int ltc3589_parse_regulators_dt(struct ltc3589 *ltc3589)
  267. {
  268. return 0;
  269. }
  270. static inline struct regulator_init_data *match_init_data(int index)
  271. {
  272. return NULL;
  273. }
  274. static inline struct device_node *match_of_node(int index)
  275. {
  276. return NULL;
  277. }
  278. #endif
  279. static bool ltc3589_writeable_reg(struct device *dev, unsigned int reg)
  280. {
  281. switch (reg) {
  282. case LTC3589_IRQSTAT:
  283. case LTC3589_SCR1:
  284. case LTC3589_OVEN:
  285. case LTC3589_SCR2:
  286. case LTC3589_VCCR:
  287. case LTC3589_CLIRQ:
  288. case LTC3589_B1DTV1:
  289. case LTC3589_B1DTV2:
  290. case LTC3589_VRRCR:
  291. case LTC3589_B2DTV1:
  292. case LTC3589_B2DTV2:
  293. case LTC3589_B3DTV1:
  294. case LTC3589_B3DTV2:
  295. case LTC3589_L2DTV1:
  296. case LTC3589_L2DTV2:
  297. return true;
  298. }
  299. return false;
  300. }
  301. static bool ltc3589_readable_reg(struct device *dev, unsigned int reg)
  302. {
  303. switch (reg) {
  304. case LTC3589_IRQSTAT:
  305. case LTC3589_SCR1:
  306. case LTC3589_OVEN:
  307. case LTC3589_SCR2:
  308. case LTC3589_PGSTAT:
  309. case LTC3589_VCCR:
  310. case LTC3589_B1DTV1:
  311. case LTC3589_B1DTV2:
  312. case LTC3589_VRRCR:
  313. case LTC3589_B2DTV1:
  314. case LTC3589_B2DTV2:
  315. case LTC3589_B3DTV1:
  316. case LTC3589_B3DTV2:
  317. case LTC3589_L2DTV1:
  318. case LTC3589_L2DTV2:
  319. return true;
  320. }
  321. return false;
  322. }
  323. static bool ltc3589_volatile_reg(struct device *dev, unsigned int reg)
  324. {
  325. switch (reg) {
  326. case LTC3589_IRQSTAT:
  327. case LTC3589_PGSTAT:
  328. case LTC3589_VCCR:
  329. return true;
  330. }
  331. return false;
  332. }
  333. static const struct reg_default ltc3589_reg_defaults[] = {
  334. { LTC3589_SCR1, 0x00 },
  335. { LTC3589_OVEN, 0x00 },
  336. { LTC3589_SCR2, 0x00 },
  337. { LTC3589_VCCR, 0x00 },
  338. { LTC3589_B1DTV1, 0x19 },
  339. { LTC3589_B1DTV2, 0x19 },
  340. { LTC3589_VRRCR, 0xff },
  341. { LTC3589_B2DTV1, 0x19 },
  342. { LTC3589_B2DTV2, 0x19 },
  343. { LTC3589_B3DTV1, 0x19 },
  344. { LTC3589_B3DTV2, 0x19 },
  345. { LTC3589_L2DTV1, 0x19 },
  346. { LTC3589_L2DTV2, 0x19 },
  347. };
  348. static const struct regmap_config ltc3589_regmap_config = {
  349. .reg_bits = 8,
  350. .val_bits = 8,
  351. .writeable_reg = ltc3589_writeable_reg,
  352. .readable_reg = ltc3589_readable_reg,
  353. .volatile_reg = ltc3589_volatile_reg,
  354. .max_register = LTC3589_L2DTV2,
  355. .reg_defaults = ltc3589_reg_defaults,
  356. .num_reg_defaults = ARRAY_SIZE(ltc3589_reg_defaults),
  357. .use_single_rw = true,
  358. .cache_type = REGCACHE_RBTREE,
  359. };
  360. static irqreturn_t ltc3589_isr(int irq, void *dev_id)
  361. {
  362. struct ltc3589 *ltc3589 = dev_id;
  363. unsigned int i, irqstat, event;
  364. regmap_read(ltc3589->regmap, LTC3589_IRQSTAT, &irqstat);
  365. if (irqstat & LTC3589_IRQSTAT_THERMAL_WARN) {
  366. event = REGULATOR_EVENT_OVER_TEMP;
  367. for (i = 0; i < LTC3589_NUM_REGULATORS; i++)
  368. regulator_notifier_call_chain(ltc3589->regulators[i],
  369. event, NULL);
  370. }
  371. if (irqstat & LTC3589_IRQSTAT_UNDERVOLT_WARN) {
  372. event = REGULATOR_EVENT_UNDER_VOLTAGE;
  373. for (i = 0; i < LTC3589_NUM_REGULATORS; i++)
  374. regulator_notifier_call_chain(ltc3589->regulators[i],
  375. event, NULL);
  376. }
  377. /* Clear warning condition */
  378. regmap_write(ltc3589->regmap, LTC3589_CLIRQ, 0);
  379. return IRQ_HANDLED;
  380. }
  381. static inline unsigned int ltc3589_scale(unsigned int uV, u32 r1, u32 r2)
  382. {
  383. uint64_t tmp;
  384. if (uV == 0)
  385. return 0;
  386. tmp = (uint64_t)uV * r1;
  387. do_div(tmp, r2);
  388. return uV + (unsigned int)tmp;
  389. }
  390. static void ltc3589_apply_fb_voltage_divider(struct ltc3589_regulator *rdesc)
  391. {
  392. struct regulator_desc *desc = &rdesc->desc;
  393. if (!rdesc->r1 || !rdesc->r2)
  394. return;
  395. desc->min_uV = ltc3589_scale(desc->min_uV, rdesc->r1, rdesc->r2);
  396. desc->uV_step = ltc3589_scale(desc->uV_step, rdesc->r1, rdesc->r2);
  397. desc->fixed_uV = ltc3589_scale(desc->fixed_uV, rdesc->r1, rdesc->r2);
  398. }
  399. static int ltc3589_probe(struct i2c_client *client,
  400. const struct i2c_device_id *id)
  401. {
  402. struct device *dev = &client->dev;
  403. struct ltc3589_regulator *descs;
  404. struct ltc3589 *ltc3589;
  405. int i, ret;
  406. ltc3589 = devm_kzalloc(dev, sizeof(*ltc3589), GFP_KERNEL);
  407. if (!ltc3589)
  408. return -ENOMEM;
  409. i2c_set_clientdata(client, ltc3589);
  410. ltc3589->variant = id->driver_data;
  411. ltc3589->dev = dev;
  412. descs = ltc3589->regulator_descs;
  413. memcpy(descs, ltc3589_regulators, sizeof(ltc3589_regulators));
  414. if (ltc3589->variant == LTC3589) {
  415. descs[LTC3589_LDO3].desc.fixed_uV = 1800000;
  416. descs[LTC3589_LDO4].desc.volt_table = ltc3589_ldo4;
  417. } else {
  418. descs[LTC3589_LDO3].desc.fixed_uV = 2800000;
  419. descs[LTC3589_LDO4].desc.volt_table = ltc3589_12_ldo4;
  420. }
  421. ltc3589->regmap = devm_regmap_init_i2c(client, &ltc3589_regmap_config);
  422. if (IS_ERR(ltc3589->regmap)) {
  423. ret = PTR_ERR(ltc3589->regmap);
  424. dev_err(dev, "failed to initialize regmap: %d\n", ret);
  425. return ret;
  426. }
  427. ret = ltc3589_parse_regulators_dt(ltc3589);
  428. if (ret)
  429. return ret;
  430. for (i = 0; i < LTC3589_NUM_REGULATORS; i++) {
  431. struct ltc3589_regulator *rdesc = &ltc3589->regulator_descs[i];
  432. struct regulator_desc *desc = &rdesc->desc;
  433. struct regulator_init_data *init_data;
  434. struct regulator_config config = { };
  435. init_data = match_init_data(i);
  436. if (i < LTC3589_LDO3)
  437. ltc3589_apply_fb_voltage_divider(rdesc);
  438. config.dev = dev;
  439. config.init_data = init_data;
  440. config.driver_data = ltc3589;
  441. config.of_node = match_of_node(i);
  442. ltc3589->regulators[i] = devm_regulator_register(dev, desc,
  443. &config);
  444. if (IS_ERR(ltc3589->regulators[i])) {
  445. ret = PTR_ERR(ltc3589->regulators[i]);
  446. dev_err(dev, "failed to register regulator %s: %d\n",
  447. desc->name, ret);
  448. return ret;
  449. }
  450. }
  451. if (client->irq) {
  452. ret = devm_request_threaded_irq(dev, client->irq, NULL,
  453. ltc3589_isr,
  454. IRQF_TRIGGER_LOW | IRQF_ONESHOT,
  455. client->name, ltc3589);
  456. if (ret) {
  457. dev_err(dev, "Failed to request IRQ: %d\n", ret);
  458. return ret;
  459. }
  460. }
  461. return 0;
  462. }
  463. static struct i2c_device_id ltc3589_i2c_id[] = {
  464. { "ltc3589", LTC3589 },
  465. { "ltc3589-1", LTC3589_1 },
  466. { "ltc3589-2", LTC3589_2 },
  467. { }
  468. };
  469. MODULE_DEVICE_TABLE(i2c, ltc3589_i2c_id);
  470. static struct i2c_driver ltc3589_driver = {
  471. .driver = {
  472. .name = DRIVER_NAME,
  473. },
  474. .probe = ltc3589_probe,
  475. .id_table = ltc3589_i2c_id,
  476. };
  477. module_i2c_driver(ltc3589_driver);
  478. MODULE_AUTHOR("Philipp Zabel <p.zabel@pengutronix.de>");
  479. MODULE_DESCRIPTION("Regulator driver for Linear Technology LTC3589(-1,2)");
  480. MODULE_LICENSE("GPL v2");