ltc3676.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. /*
  2. * Copyright (C) 2016 Gateworks Corporation, Inc. All Rights Reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2
  6. * as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. */
  14. #include <linux/i2c.h>
  15. #include <linux/init.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/module.h>
  18. #include <linux/kernel.h>
  19. #include <linux/of.h>
  20. #include <linux/regmap.h>
  21. #include <linux/regulator/driver.h>
  22. #include <linux/regulator/machine.h>
  23. #include <linux/regulator/of_regulator.h>
  24. #define DRIVER_NAME "ltc3676"
  25. /* LTC3676 Registers */
  26. #define LTC3676_BUCK1 0x01
  27. #define LTC3676_BUCK2 0x02
  28. #define LTC3676_BUCK3 0x03
  29. #define LTC3676_BUCK4 0x04
  30. #define LTC3676_LDOA 0x05
  31. #define LTC3676_LDOB 0x06
  32. #define LTC3676_SQD1 0x07
  33. #define LTC3676_SQD2 0x08
  34. #define LTC3676_CNTRL 0x09
  35. #define LTC3676_DVB1A 0x0A
  36. #define LTC3676_DVB1B 0x0B
  37. #define LTC3676_DVB2A 0x0C
  38. #define LTC3676_DVB2B 0x0D
  39. #define LTC3676_DVB3A 0x0E
  40. #define LTC3676_DVB3B 0x0F
  41. #define LTC3676_DVB4A 0x10
  42. #define LTC3676_DVB4B 0x11
  43. #define LTC3676_MSKIRQ 0x12
  44. #define LTC3676_MSKPG 0x13
  45. #define LTC3676_USER 0x14
  46. #define LTC3676_IRQSTAT 0x15
  47. #define LTC3676_PGSTATL 0x16
  48. #define LTC3676_PGSTATRT 0x17
  49. #define LTC3676_HRST 0x1E
  50. #define LTC3676_CLIRQ 0x1F
  51. #define LTC3676_DVBxA_REF_SELECT BIT(5)
  52. #define LTC3676_IRQSTAT_PGOOD_TIMEOUT BIT(3)
  53. #define LTC3676_IRQSTAT_UNDERVOLT_WARN BIT(4)
  54. #define LTC3676_IRQSTAT_UNDERVOLT_FAULT BIT(5)
  55. #define LTC3676_IRQSTAT_THERMAL_WARN BIT(6)
  56. #define LTC3676_IRQSTAT_THERMAL_FAULT BIT(7)
  57. enum ltc3676_reg {
  58. LTC3676_SW1,
  59. LTC3676_SW2,
  60. LTC3676_SW3,
  61. LTC3676_SW4,
  62. LTC3676_LDO1,
  63. LTC3676_LDO2,
  64. LTC3676_LDO3,
  65. LTC3676_LDO4,
  66. LTC3676_NUM_REGULATORS,
  67. };
  68. struct ltc3676 {
  69. struct regmap *regmap;
  70. struct device *dev;
  71. struct regulator_desc regulator_descs[LTC3676_NUM_REGULATORS];
  72. struct regulator_dev *regulators[LTC3676_NUM_REGULATORS];
  73. };
  74. static int ltc3676_set_suspend_voltage(struct regulator_dev *rdev, int uV)
  75. {
  76. struct ltc3676 *ltc3676 = rdev_get_drvdata(rdev);
  77. struct device *dev = ltc3676->dev;
  78. int dcdc = rdev_get_id(rdev);
  79. int sel;
  80. dev_dbg(dev, "%s id=%d uV=%d\n", __func__, dcdc, uV);
  81. sel = regulator_map_voltage_linear(rdev, uV, uV);
  82. if (sel < 0)
  83. return sel;
  84. /* DVBB register follows right after the corresponding DVBA register */
  85. return regmap_update_bits(ltc3676->regmap, rdev->desc->vsel_reg + 1,
  86. rdev->desc->vsel_mask, sel);
  87. }
  88. static int ltc3676_set_suspend_mode(struct regulator_dev *rdev,
  89. unsigned int mode)
  90. {
  91. struct ltc3676 *ltc3676= rdev_get_drvdata(rdev);
  92. struct device *dev = ltc3676->dev;
  93. int mask, val;
  94. int dcdc = rdev_get_id(rdev);
  95. dev_dbg(dev, "%s id=%d mode=%d\n", __func__, dcdc, mode);
  96. mask = LTC3676_DVBxA_REF_SELECT;
  97. switch (mode) {
  98. case REGULATOR_MODE_STANDBY:
  99. val = 0; /* select DVBxA */
  100. break;
  101. case REGULATOR_MODE_NORMAL:
  102. val = LTC3676_DVBxA_REF_SELECT; /* select DVBxB */
  103. break;
  104. default:
  105. dev_warn(&rdev->dev, "%s: regulator mode: 0x%x not supported\n",
  106. rdev->desc->name, mode);
  107. return -EINVAL;
  108. }
  109. return regmap_update_bits(ltc3676->regmap, rdev->desc->vsel_reg,
  110. mask, val);
  111. }
  112. static inline unsigned int ltc3676_scale(unsigned int uV, u32 r1, u32 r2)
  113. {
  114. uint64_t tmp;
  115. if (uV == 0)
  116. return 0;
  117. tmp = (uint64_t)uV * r1;
  118. do_div(tmp, r2);
  119. return uV + (unsigned int)tmp;
  120. }
  121. static int ltc3676_of_parse_cb(struct device_node *np,
  122. const struct regulator_desc *desc,
  123. struct regulator_config *config)
  124. {
  125. struct ltc3676 *ltc3676 = config->driver_data;
  126. struct regulator_desc *rdesc = &ltc3676->regulator_descs[desc->id];
  127. u32 r[2];
  128. int ret;
  129. /* LDO3 has a fixed output */
  130. if (desc->id == LTC3676_LDO3)
  131. return 0;
  132. ret = of_property_read_u32_array(np, "lltc,fb-voltage-divider", r, 2);
  133. if (ret) {
  134. dev_err(ltc3676->dev, "Failed to parse voltage divider: %d\n",
  135. ret);
  136. return ret;
  137. }
  138. rdesc->min_uV = ltc3676_scale(desc->min_uV, r[0], r[1]);
  139. rdesc->uV_step = ltc3676_scale(desc->uV_step, r[0], r[1]);
  140. rdesc->fixed_uV = ltc3676_scale(desc->fixed_uV, r[0], r[1]);
  141. return 0;
  142. }
  143. /* SW1, SW2, SW3, SW4 linear 0.8V-3.3V with scalar via R1/R2 feeback res */
  144. static const struct regulator_ops ltc3676_linear_regulator_ops = {
  145. .enable = regulator_enable_regmap,
  146. .disable = regulator_disable_regmap,
  147. .is_enabled = regulator_is_enabled_regmap,
  148. .list_voltage = regulator_list_voltage_linear,
  149. .set_voltage_sel = regulator_set_voltage_sel_regmap,
  150. .get_voltage_sel = regulator_get_voltage_sel_regmap,
  151. .set_suspend_voltage = ltc3676_set_suspend_voltage,
  152. .set_suspend_mode = ltc3676_set_suspend_mode,
  153. };
  154. /* LDO1 always on fixed 0.8V-3.3V via scalar via R1/R2 feeback res */
  155. static const struct regulator_ops ltc3676_fixed_standby_regulator_ops = {
  156. };
  157. /* LDO2, LDO3 fixed (LDO2 has external scalar via R1/R2 feedback res) */
  158. static const struct regulator_ops ltc3676_fixed_regulator_ops = {
  159. .enable = regulator_enable_regmap,
  160. .disable = regulator_disable_regmap,
  161. .is_enabled = regulator_is_enabled_regmap,
  162. };
  163. #define LTC3676_REG(_id, _name, _ops, en_reg, en_bit, dvba_reg, dvb_mask) \
  164. [LTC3676_ ## _id] = { \
  165. .name = #_name, \
  166. .of_match = of_match_ptr(#_name), \
  167. .regulators_node = of_match_ptr("regulators"), \
  168. .of_parse_cb = ltc3676_of_parse_cb, \
  169. .n_voltages = (dvb_mask) + 1, \
  170. .min_uV = (dvba_reg) ? 412500 : 0, \
  171. .uV_step = (dvba_reg) ? 12500 : 0, \
  172. .ramp_delay = (dvba_reg) ? 800 : 0, \
  173. .fixed_uV = (dvb_mask) ? 0 : 725000, \
  174. .ops = &ltc3676_ ## _ops ## _regulator_ops, \
  175. .type = REGULATOR_VOLTAGE, \
  176. .id = LTC3676_ ## _id, \
  177. .owner = THIS_MODULE, \
  178. .vsel_reg = (dvba_reg), \
  179. .vsel_mask = (dvb_mask), \
  180. .enable_reg = (en_reg), \
  181. .enable_mask = (1 << en_bit), \
  182. }
  183. #define LTC3676_LINEAR_REG(_id, _name, _en, _dvba) \
  184. LTC3676_REG(_id, _name, linear, \
  185. LTC3676_ ## _en, 7, \
  186. LTC3676_ ## _dvba, 0x1f)
  187. #define LTC3676_FIXED_REG(_id, _name, _en_reg, _en_bit) \
  188. LTC3676_REG(_id, _name, fixed, LTC3676_ ## _en_reg, _en_bit, 0, 0)
  189. static struct regulator_desc ltc3676_regulators[LTC3676_NUM_REGULATORS] = {
  190. LTC3676_LINEAR_REG(SW1, sw1, BUCK1, DVB1A),
  191. LTC3676_LINEAR_REG(SW2, sw2, BUCK2, DVB2A),
  192. LTC3676_LINEAR_REG(SW3, sw3, BUCK3, DVB3A),
  193. LTC3676_LINEAR_REG(SW4, sw4, BUCK4, DVB4A),
  194. LTC3676_REG(LDO1, ldo1, fixed_standby, 0, 0, 0, 0),
  195. LTC3676_FIXED_REG(LDO2, ldo2, LDOA, 2),
  196. LTC3676_FIXED_REG(LDO3, ldo3, LDOA, 5),
  197. LTC3676_FIXED_REG(LDO4, ldo4, LDOB, 2),
  198. };
  199. static bool ltc3676_writeable_reg(struct device *dev, unsigned int reg)
  200. {
  201. switch (reg) {
  202. case LTC3676_IRQSTAT:
  203. case LTC3676_BUCK1:
  204. case LTC3676_BUCK2:
  205. case LTC3676_BUCK3:
  206. case LTC3676_BUCK4:
  207. case LTC3676_LDOA:
  208. case LTC3676_LDOB:
  209. case LTC3676_SQD1:
  210. case LTC3676_SQD2:
  211. case LTC3676_CNTRL:
  212. case LTC3676_DVB1A:
  213. case LTC3676_DVB1B:
  214. case LTC3676_DVB2A:
  215. case LTC3676_DVB2B:
  216. case LTC3676_DVB3A:
  217. case LTC3676_DVB3B:
  218. case LTC3676_DVB4A:
  219. case LTC3676_DVB4B:
  220. case LTC3676_MSKIRQ:
  221. case LTC3676_MSKPG:
  222. case LTC3676_USER:
  223. case LTC3676_HRST:
  224. case LTC3676_CLIRQ:
  225. return true;
  226. }
  227. return false;
  228. }
  229. static bool ltc3676_readable_reg(struct device *dev, unsigned int reg)
  230. {
  231. switch (reg) {
  232. case LTC3676_IRQSTAT:
  233. case LTC3676_BUCK1:
  234. case LTC3676_BUCK2:
  235. case LTC3676_BUCK3:
  236. case LTC3676_BUCK4:
  237. case LTC3676_LDOA:
  238. case LTC3676_LDOB:
  239. case LTC3676_SQD1:
  240. case LTC3676_SQD2:
  241. case LTC3676_CNTRL:
  242. case LTC3676_DVB1A:
  243. case LTC3676_DVB1B:
  244. case LTC3676_DVB2A:
  245. case LTC3676_DVB2B:
  246. case LTC3676_DVB3A:
  247. case LTC3676_DVB3B:
  248. case LTC3676_DVB4A:
  249. case LTC3676_DVB4B:
  250. case LTC3676_MSKIRQ:
  251. case LTC3676_MSKPG:
  252. case LTC3676_USER:
  253. case LTC3676_HRST:
  254. case LTC3676_CLIRQ:
  255. return true;
  256. }
  257. return false;
  258. }
  259. static bool ltc3676_volatile_reg(struct device *dev, unsigned int reg)
  260. {
  261. switch (reg) {
  262. case LTC3676_IRQSTAT:
  263. case LTC3676_PGSTATL:
  264. case LTC3676_PGSTATRT:
  265. return true;
  266. }
  267. return false;
  268. }
  269. static const struct regmap_config ltc3676_regmap_config = {
  270. .reg_bits = 8,
  271. .val_bits = 8,
  272. .writeable_reg = ltc3676_writeable_reg,
  273. .readable_reg = ltc3676_readable_reg,
  274. .volatile_reg = ltc3676_volatile_reg,
  275. .max_register = LTC3676_CLIRQ,
  276. .use_single_rw = true,
  277. .cache_type = REGCACHE_RBTREE,
  278. };
  279. static irqreturn_t ltc3676_isr(int irq, void *dev_id)
  280. {
  281. struct ltc3676 *ltc3676 = dev_id;
  282. struct device *dev = ltc3676->dev;
  283. unsigned int i, irqstat, event;
  284. regmap_read(ltc3676->regmap, LTC3676_IRQSTAT, &irqstat);
  285. dev_dbg(dev, "irq%d irqstat=0x%02x\n", irq, irqstat);
  286. if (irqstat & LTC3676_IRQSTAT_THERMAL_WARN) {
  287. dev_warn(dev, "Over-temperature Warning\n");
  288. event = REGULATOR_EVENT_OVER_TEMP;
  289. for (i = 0; i < LTC3676_NUM_REGULATORS; i++)
  290. regulator_notifier_call_chain(ltc3676->regulators[i],
  291. event, NULL);
  292. }
  293. if (irqstat & LTC3676_IRQSTAT_UNDERVOLT_WARN) {
  294. dev_info(dev, "Undervoltage Warning\n");
  295. event = REGULATOR_EVENT_UNDER_VOLTAGE;
  296. for (i = 0; i < LTC3676_NUM_REGULATORS; i++)
  297. regulator_notifier_call_chain(ltc3676->regulators[i],
  298. event, NULL);
  299. }
  300. /* Clear warning condition */
  301. regmap_write(ltc3676->regmap, LTC3676_CLIRQ, 0);
  302. return IRQ_HANDLED;
  303. }
  304. static int ltc3676_regulator_probe(struct i2c_client *client,
  305. const struct i2c_device_id *id)
  306. {
  307. struct device *dev = &client->dev;
  308. struct regulator_init_data *init_data = dev_get_platdata(dev);
  309. struct regulator_desc *descs;
  310. struct ltc3676 *ltc3676;
  311. int i, ret;
  312. ltc3676 = devm_kzalloc(dev, sizeof(*ltc3676), GFP_KERNEL);
  313. if (!ltc3676)
  314. return -ENOMEM;
  315. i2c_set_clientdata(client, ltc3676);
  316. ltc3676->dev = dev;
  317. descs = ltc3676->regulator_descs;
  318. memcpy(descs, ltc3676_regulators, sizeof(ltc3676_regulators));
  319. descs[LTC3676_LDO3].fixed_uV = 1800000; /* LDO3 is fixed 1.8V */
  320. ltc3676->regmap = devm_regmap_init_i2c(client, &ltc3676_regmap_config);
  321. if (IS_ERR(ltc3676->regmap)) {
  322. ret = PTR_ERR(ltc3676->regmap);
  323. dev_err(dev, "failed to initialize regmap: %d\n", ret);
  324. return ret;
  325. }
  326. for (i = 0; i < LTC3676_NUM_REGULATORS; i++) {
  327. struct regulator_desc *desc = &ltc3676->regulator_descs[i];
  328. struct regulator_config config = { };
  329. if (init_data)
  330. config.init_data = &init_data[i];
  331. config.dev = dev;
  332. config.driver_data = ltc3676;
  333. ltc3676->regulators[i] = devm_regulator_register(dev, desc,
  334. &config);
  335. if (IS_ERR(ltc3676->regulators[i])) {
  336. ret = PTR_ERR(ltc3676->regulators[i]);
  337. dev_err(dev, "failed to register regulator %s: %d\n",
  338. desc->name, ret);
  339. return ret;
  340. }
  341. }
  342. regmap_write(ltc3676->regmap, LTC3676_CLIRQ, 0);
  343. if (client->irq) {
  344. ret = devm_request_threaded_irq(dev, client->irq, NULL,
  345. ltc3676_isr,
  346. IRQF_TRIGGER_LOW | IRQF_ONESHOT,
  347. client->name, ltc3676);
  348. if (ret) {
  349. dev_err(dev, "Failed to request IRQ: %d\n", ret);
  350. return ret;
  351. }
  352. }
  353. return 0;
  354. }
  355. static const struct i2c_device_id ltc3676_i2c_id[] = {
  356. { "ltc3676" },
  357. { }
  358. };
  359. MODULE_DEVICE_TABLE(i2c, ltc3676_i2c_id);
  360. static const struct of_device_id ltc3676_of_match[] = {
  361. { .compatible = "lltc,ltc3676" },
  362. { },
  363. };
  364. MODULE_DEVICE_TABLE(of, ltc3676_of_match);
  365. static struct i2c_driver ltc3676_driver = {
  366. .driver = {
  367. .name = DRIVER_NAME,
  368. .of_match_table = of_match_ptr(ltc3676_of_match),
  369. },
  370. .probe = ltc3676_regulator_probe,
  371. .id_table = ltc3676_i2c_id,
  372. };
  373. module_i2c_driver(ltc3676_driver);
  374. MODULE_AUTHOR("Tim Harvey <tharvey@gateworks.com>");
  375. MODULE_DESCRIPTION("Regulator driver for Linear Technology LTC1376");
  376. MODULE_LICENSE("GPL v2");