lp3971.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. /*
  2. * Regulator driver for National Semiconductors LP3971 PMIC chip
  3. *
  4. * Copyright (C) 2009 Samsung Electronics
  5. * Author: Marek Szyprowski <m.szyprowski@samsung.com>
  6. *
  7. * Based on wm8350.c
  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 as
  11. * published by the Free Software Foundation.
  12. *
  13. */
  14. #include <linux/bug.h>
  15. #include <linux/err.h>
  16. #include <linux/i2c.h>
  17. #include <linux/kernel.h>
  18. #include <linux/module.h>
  19. #include <linux/regulator/driver.h>
  20. #include <linux/regulator/lp3971.h>
  21. #include <linux/slab.h>
  22. struct lp3971 {
  23. struct device *dev;
  24. struct mutex io_lock;
  25. struct i2c_client *i2c;
  26. };
  27. static u8 lp3971_reg_read(struct lp3971 *lp3971, u8 reg);
  28. static int lp3971_set_bits(struct lp3971 *lp3971, u8 reg, u16 mask, u16 val);
  29. #define LP3971_SYS_CONTROL1_REG 0x07
  30. /* System control register 1 initial value,
  31. bits 4 and 5 are EPROM programmable */
  32. #define SYS_CONTROL1_INIT_VAL 0x40
  33. #define SYS_CONTROL1_INIT_MASK 0xCF
  34. #define LP3971_BUCK_VOL_ENABLE_REG 0x10
  35. #define LP3971_BUCK_VOL_CHANGE_REG 0x20
  36. /* Voltage control registers shift:
  37. LP3971_BUCK1 -> 0
  38. LP3971_BUCK2 -> 4
  39. LP3971_BUCK3 -> 6
  40. */
  41. #define BUCK_VOL_CHANGE_SHIFT(x) (((!!x) << 2) | (x & ~0x01))
  42. #define BUCK_VOL_CHANGE_FLAG_GO 0x01
  43. #define BUCK_VOL_CHANGE_FLAG_TARGET 0x02
  44. #define BUCK_VOL_CHANGE_FLAG_MASK 0x03
  45. #define LP3971_BUCK1_BASE 0x23
  46. #define LP3971_BUCK2_BASE 0x29
  47. #define LP3971_BUCK3_BASE 0x32
  48. static const int buck_base_addr[] = {
  49. LP3971_BUCK1_BASE,
  50. LP3971_BUCK2_BASE,
  51. LP3971_BUCK3_BASE,
  52. };
  53. #define LP3971_BUCK_TARGET_VOL1_REG(x) (buck_base_addr[x])
  54. #define LP3971_BUCK_TARGET_VOL2_REG(x) (buck_base_addr[x]+1)
  55. static const unsigned int buck_voltage_map[] = {
  56. 0, 800000, 850000, 900000, 950000, 1000000, 1050000, 1100000,
  57. 1150000, 1200000, 1250000, 1300000, 1350000, 1400000, 1450000, 1500000,
  58. 1550000, 1600000, 1650000, 1700000, 1800000, 1900000, 2500000, 2800000,
  59. 3000000, 3300000,
  60. };
  61. #define BUCK_TARGET_VOL_MASK 0x3f
  62. #define LP3971_BUCK_RAMP_REG(x) (buck_base_addr[x]+2)
  63. #define LP3971_LDO_ENABLE_REG 0x12
  64. #define LP3971_LDO_VOL_CONTR_BASE 0x39
  65. /* Voltage control registers:
  66. LP3971_LDO1 -> LP3971_LDO_VOL_CONTR_BASE + 0
  67. LP3971_LDO2 -> LP3971_LDO_VOL_CONTR_BASE + 0
  68. LP3971_LDO3 -> LP3971_LDO_VOL_CONTR_BASE + 1
  69. LP3971_LDO4 -> LP3971_LDO_VOL_CONTR_BASE + 1
  70. LP3971_LDO5 -> LP3971_LDO_VOL_CONTR_BASE + 2
  71. */
  72. #define LP3971_LDO_VOL_CONTR_REG(x) (LP3971_LDO_VOL_CONTR_BASE + (x >> 1))
  73. /* Voltage control registers shift:
  74. LP3971_LDO1 -> 0, LP3971_LDO2 -> 4
  75. LP3971_LDO3 -> 0, LP3971_LDO4 -> 4
  76. LP3971_LDO5 -> 0
  77. */
  78. #define LDO_VOL_CONTR_SHIFT(x) ((x & 1) << 2)
  79. #define LDO_VOL_CONTR_MASK 0x0f
  80. static const unsigned int ldo45_voltage_map[] = {
  81. 1000000, 1050000, 1100000, 1150000, 1200000, 1250000, 1300000, 1350000,
  82. 1400000, 1500000, 1800000, 1900000, 2500000, 2800000, 3000000, 3300000,
  83. };
  84. static const unsigned int ldo123_voltage_map[] = {
  85. 1800000, 1900000, 2000000, 2100000, 2200000, 2300000, 2400000, 2500000,
  86. 2600000, 2700000, 2800000, 2900000, 3000000, 3100000, 3200000, 3300000,
  87. };
  88. #define LDO_VOL_MIN_IDX 0x00
  89. #define LDO_VOL_MAX_IDX 0x0f
  90. static int lp3971_ldo_is_enabled(struct regulator_dev *dev)
  91. {
  92. struct lp3971 *lp3971 = rdev_get_drvdata(dev);
  93. int ldo = rdev_get_id(dev) - LP3971_LDO1;
  94. u16 mask = 1 << (1 + ldo);
  95. u16 val;
  96. val = lp3971_reg_read(lp3971, LP3971_LDO_ENABLE_REG);
  97. return (val & mask) != 0;
  98. }
  99. static int lp3971_ldo_enable(struct regulator_dev *dev)
  100. {
  101. struct lp3971 *lp3971 = rdev_get_drvdata(dev);
  102. int ldo = rdev_get_id(dev) - LP3971_LDO1;
  103. u16 mask = 1 << (1 + ldo);
  104. return lp3971_set_bits(lp3971, LP3971_LDO_ENABLE_REG, mask, mask);
  105. }
  106. static int lp3971_ldo_disable(struct regulator_dev *dev)
  107. {
  108. struct lp3971 *lp3971 = rdev_get_drvdata(dev);
  109. int ldo = rdev_get_id(dev) - LP3971_LDO1;
  110. u16 mask = 1 << (1 + ldo);
  111. return lp3971_set_bits(lp3971, LP3971_LDO_ENABLE_REG, mask, 0);
  112. }
  113. static int lp3971_ldo_get_voltage_sel(struct regulator_dev *dev)
  114. {
  115. struct lp3971 *lp3971 = rdev_get_drvdata(dev);
  116. int ldo = rdev_get_id(dev) - LP3971_LDO1;
  117. u16 val, reg;
  118. reg = lp3971_reg_read(lp3971, LP3971_LDO_VOL_CONTR_REG(ldo));
  119. val = (reg >> LDO_VOL_CONTR_SHIFT(ldo)) & LDO_VOL_CONTR_MASK;
  120. return val;
  121. }
  122. static int lp3971_ldo_set_voltage_sel(struct regulator_dev *dev,
  123. unsigned int selector)
  124. {
  125. struct lp3971 *lp3971 = rdev_get_drvdata(dev);
  126. int ldo = rdev_get_id(dev) - LP3971_LDO1;
  127. return lp3971_set_bits(lp3971, LP3971_LDO_VOL_CONTR_REG(ldo),
  128. LDO_VOL_CONTR_MASK << LDO_VOL_CONTR_SHIFT(ldo),
  129. selector << LDO_VOL_CONTR_SHIFT(ldo));
  130. }
  131. static struct regulator_ops lp3971_ldo_ops = {
  132. .list_voltage = regulator_list_voltage_table,
  133. .map_voltage = regulator_map_voltage_ascend,
  134. .is_enabled = lp3971_ldo_is_enabled,
  135. .enable = lp3971_ldo_enable,
  136. .disable = lp3971_ldo_disable,
  137. .get_voltage_sel = lp3971_ldo_get_voltage_sel,
  138. .set_voltage_sel = lp3971_ldo_set_voltage_sel,
  139. };
  140. static int lp3971_dcdc_is_enabled(struct regulator_dev *dev)
  141. {
  142. struct lp3971 *lp3971 = rdev_get_drvdata(dev);
  143. int buck = rdev_get_id(dev) - LP3971_DCDC1;
  144. u16 mask = 1 << (buck * 2);
  145. u16 val;
  146. val = lp3971_reg_read(lp3971, LP3971_BUCK_VOL_ENABLE_REG);
  147. return (val & mask) != 0;
  148. }
  149. static int lp3971_dcdc_enable(struct regulator_dev *dev)
  150. {
  151. struct lp3971 *lp3971 = rdev_get_drvdata(dev);
  152. int buck = rdev_get_id(dev) - LP3971_DCDC1;
  153. u16 mask = 1 << (buck * 2);
  154. return lp3971_set_bits(lp3971, LP3971_BUCK_VOL_ENABLE_REG, mask, mask);
  155. }
  156. static int lp3971_dcdc_disable(struct regulator_dev *dev)
  157. {
  158. struct lp3971 *lp3971 = rdev_get_drvdata(dev);
  159. int buck = rdev_get_id(dev) - LP3971_DCDC1;
  160. u16 mask = 1 << (buck * 2);
  161. return lp3971_set_bits(lp3971, LP3971_BUCK_VOL_ENABLE_REG, mask, 0);
  162. }
  163. static int lp3971_dcdc_get_voltage_sel(struct regulator_dev *dev)
  164. {
  165. struct lp3971 *lp3971 = rdev_get_drvdata(dev);
  166. int buck = rdev_get_id(dev) - LP3971_DCDC1;
  167. u16 reg;
  168. reg = lp3971_reg_read(lp3971, LP3971_BUCK_TARGET_VOL1_REG(buck));
  169. reg &= BUCK_TARGET_VOL_MASK;
  170. return reg;
  171. }
  172. static int lp3971_dcdc_set_voltage_sel(struct regulator_dev *dev,
  173. unsigned int selector)
  174. {
  175. struct lp3971 *lp3971 = rdev_get_drvdata(dev);
  176. int buck = rdev_get_id(dev) - LP3971_DCDC1;
  177. int ret;
  178. ret = lp3971_set_bits(lp3971, LP3971_BUCK_TARGET_VOL1_REG(buck),
  179. BUCK_TARGET_VOL_MASK, selector);
  180. if (ret)
  181. return ret;
  182. ret = lp3971_set_bits(lp3971, LP3971_BUCK_VOL_CHANGE_REG,
  183. BUCK_VOL_CHANGE_FLAG_MASK << BUCK_VOL_CHANGE_SHIFT(buck),
  184. BUCK_VOL_CHANGE_FLAG_GO << BUCK_VOL_CHANGE_SHIFT(buck));
  185. if (ret)
  186. return ret;
  187. return lp3971_set_bits(lp3971, LP3971_BUCK_VOL_CHANGE_REG,
  188. BUCK_VOL_CHANGE_FLAG_MASK << BUCK_VOL_CHANGE_SHIFT(buck),
  189. 0 << BUCK_VOL_CHANGE_SHIFT(buck));
  190. }
  191. static struct regulator_ops lp3971_dcdc_ops = {
  192. .list_voltage = regulator_list_voltage_table,
  193. .map_voltage = regulator_map_voltage_ascend,
  194. .is_enabled = lp3971_dcdc_is_enabled,
  195. .enable = lp3971_dcdc_enable,
  196. .disable = lp3971_dcdc_disable,
  197. .get_voltage_sel = lp3971_dcdc_get_voltage_sel,
  198. .set_voltage_sel = lp3971_dcdc_set_voltage_sel,
  199. };
  200. static const struct regulator_desc regulators[] = {
  201. {
  202. .name = "LDO1",
  203. .id = LP3971_LDO1,
  204. .ops = &lp3971_ldo_ops,
  205. .n_voltages = ARRAY_SIZE(ldo123_voltage_map),
  206. .volt_table = ldo123_voltage_map,
  207. .type = REGULATOR_VOLTAGE,
  208. .owner = THIS_MODULE,
  209. },
  210. {
  211. .name = "LDO2",
  212. .id = LP3971_LDO2,
  213. .ops = &lp3971_ldo_ops,
  214. .n_voltages = ARRAY_SIZE(ldo123_voltage_map),
  215. .volt_table = ldo123_voltage_map,
  216. .type = REGULATOR_VOLTAGE,
  217. .owner = THIS_MODULE,
  218. },
  219. {
  220. .name = "LDO3",
  221. .id = LP3971_LDO3,
  222. .ops = &lp3971_ldo_ops,
  223. .n_voltages = ARRAY_SIZE(ldo123_voltage_map),
  224. .volt_table = ldo123_voltage_map,
  225. .type = REGULATOR_VOLTAGE,
  226. .owner = THIS_MODULE,
  227. },
  228. {
  229. .name = "LDO4",
  230. .id = LP3971_LDO4,
  231. .ops = &lp3971_ldo_ops,
  232. .n_voltages = ARRAY_SIZE(ldo45_voltage_map),
  233. .volt_table = ldo45_voltage_map,
  234. .type = REGULATOR_VOLTAGE,
  235. .owner = THIS_MODULE,
  236. },
  237. {
  238. .name = "LDO5",
  239. .id = LP3971_LDO5,
  240. .ops = &lp3971_ldo_ops,
  241. .n_voltages = ARRAY_SIZE(ldo45_voltage_map),
  242. .volt_table = ldo45_voltage_map,
  243. .type = REGULATOR_VOLTAGE,
  244. .owner = THIS_MODULE,
  245. },
  246. {
  247. .name = "DCDC1",
  248. .id = LP3971_DCDC1,
  249. .ops = &lp3971_dcdc_ops,
  250. .n_voltages = ARRAY_SIZE(buck_voltage_map),
  251. .volt_table = buck_voltage_map,
  252. .type = REGULATOR_VOLTAGE,
  253. .owner = THIS_MODULE,
  254. },
  255. {
  256. .name = "DCDC2",
  257. .id = LP3971_DCDC2,
  258. .ops = &lp3971_dcdc_ops,
  259. .n_voltages = ARRAY_SIZE(buck_voltage_map),
  260. .volt_table = buck_voltage_map,
  261. .type = REGULATOR_VOLTAGE,
  262. .owner = THIS_MODULE,
  263. },
  264. {
  265. .name = "DCDC3",
  266. .id = LP3971_DCDC3,
  267. .ops = &lp3971_dcdc_ops,
  268. .n_voltages = ARRAY_SIZE(buck_voltage_map),
  269. .volt_table = buck_voltage_map,
  270. .type = REGULATOR_VOLTAGE,
  271. .owner = THIS_MODULE,
  272. },
  273. };
  274. static int lp3971_i2c_read(struct i2c_client *i2c, char reg, int count,
  275. u16 *dest)
  276. {
  277. int ret;
  278. if (count != 1)
  279. return -EIO;
  280. ret = i2c_smbus_read_byte_data(i2c, reg);
  281. if (ret < 0)
  282. return ret;
  283. *dest = ret;
  284. return 0;
  285. }
  286. static int lp3971_i2c_write(struct i2c_client *i2c, char reg, int count,
  287. const u16 *src)
  288. {
  289. if (count != 1)
  290. return -EIO;
  291. return i2c_smbus_write_byte_data(i2c, reg, *src);
  292. }
  293. static u8 lp3971_reg_read(struct lp3971 *lp3971, u8 reg)
  294. {
  295. u16 val = 0;
  296. mutex_lock(&lp3971->io_lock);
  297. lp3971_i2c_read(lp3971->i2c, reg, 1, &val);
  298. dev_dbg(lp3971->dev, "reg read 0x%02x -> 0x%02x\n", (int)reg,
  299. (unsigned)val&0xff);
  300. mutex_unlock(&lp3971->io_lock);
  301. return val & 0xff;
  302. }
  303. static int lp3971_set_bits(struct lp3971 *lp3971, u8 reg, u16 mask, u16 val)
  304. {
  305. u16 tmp;
  306. int ret;
  307. mutex_lock(&lp3971->io_lock);
  308. ret = lp3971_i2c_read(lp3971->i2c, reg, 1, &tmp);
  309. if (ret == 0) {
  310. tmp = (tmp & ~mask) | val;
  311. ret = lp3971_i2c_write(lp3971->i2c, reg, 1, &tmp);
  312. dev_dbg(lp3971->dev, "reg write 0x%02x -> 0x%02x\n", (int)reg,
  313. (unsigned)val&0xff);
  314. }
  315. mutex_unlock(&lp3971->io_lock);
  316. return ret;
  317. }
  318. static int setup_regulators(struct lp3971 *lp3971,
  319. struct lp3971_platform_data *pdata)
  320. {
  321. int i, err;
  322. /* Instantiate the regulators */
  323. for (i = 0; i < pdata->num_regulators; i++) {
  324. struct regulator_config config = { };
  325. struct lp3971_regulator_subdev *reg = &pdata->regulators[i];
  326. struct regulator_dev *rdev;
  327. config.dev = lp3971->dev;
  328. config.init_data = reg->initdata;
  329. config.driver_data = lp3971;
  330. rdev = devm_regulator_register(lp3971->dev,
  331. &regulators[reg->id], &config);
  332. if (IS_ERR(rdev)) {
  333. err = PTR_ERR(rdev);
  334. dev_err(lp3971->dev, "regulator init failed: %d\n",
  335. err);
  336. return err;
  337. }
  338. }
  339. return 0;
  340. }
  341. static int lp3971_i2c_probe(struct i2c_client *i2c,
  342. const struct i2c_device_id *id)
  343. {
  344. struct lp3971 *lp3971;
  345. struct lp3971_platform_data *pdata = dev_get_platdata(&i2c->dev);
  346. int ret;
  347. u16 val;
  348. if (!pdata) {
  349. dev_dbg(&i2c->dev, "No platform init data supplied\n");
  350. return -ENODEV;
  351. }
  352. lp3971 = devm_kzalloc(&i2c->dev, sizeof(struct lp3971), GFP_KERNEL);
  353. if (lp3971 == NULL)
  354. return -ENOMEM;
  355. lp3971->i2c = i2c;
  356. lp3971->dev = &i2c->dev;
  357. mutex_init(&lp3971->io_lock);
  358. /* Detect LP3971 */
  359. ret = lp3971_i2c_read(i2c, LP3971_SYS_CONTROL1_REG, 1, &val);
  360. if (ret == 0 && (val & SYS_CONTROL1_INIT_MASK) != SYS_CONTROL1_INIT_VAL)
  361. ret = -ENODEV;
  362. if (ret < 0) {
  363. dev_err(&i2c->dev, "failed to detect device\n");
  364. return ret;
  365. }
  366. ret = setup_regulators(lp3971, pdata);
  367. if (ret < 0)
  368. return ret;
  369. i2c_set_clientdata(i2c, lp3971);
  370. return 0;
  371. }
  372. static const struct i2c_device_id lp3971_i2c_id[] = {
  373. { "lp3971", 0 },
  374. { }
  375. };
  376. MODULE_DEVICE_TABLE(i2c, lp3971_i2c_id);
  377. static struct i2c_driver lp3971_i2c_driver = {
  378. .driver = {
  379. .name = "LP3971",
  380. },
  381. .probe = lp3971_i2c_probe,
  382. .id_table = lp3971_i2c_id,
  383. };
  384. module_i2c_driver(lp3971_i2c_driver);
  385. MODULE_LICENSE("GPL");
  386. MODULE_AUTHOR("Marek Szyprowski <m.szyprowski@samsung.com>");
  387. MODULE_DESCRIPTION("LP3971 PMIC driver");