lp3971.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  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/regulator/driver.h>
  19. #include <linux/regulator/lp3971.h>
  20. #include <linux/slab.h>
  21. struct lp3971 {
  22. struct device *dev;
  23. struct mutex io_lock;
  24. struct i2c_client *i2c;
  25. int num_regulators;
  26. struct regulator_dev **rdev;
  27. };
  28. static u8 lp3971_reg_read(struct lp3971 *lp3971, u8 reg);
  29. static int lp3971_set_bits(struct lp3971 *lp3971, u8 reg, u16 mask, u16 val);
  30. #define LP3971_SYS_CONTROL1_REG 0x07
  31. /* System control register 1 initial value,
  32. bits 4 and 5 are EPROM programmable */
  33. #define SYS_CONTROL1_INIT_VAL 0x40
  34. #define SYS_CONTROL1_INIT_MASK 0xCF
  35. #define LP3971_BUCK_VOL_ENABLE_REG 0x10
  36. #define LP3971_BUCK_VOL_CHANGE_REG 0x20
  37. /* Voltage control registers shift:
  38. LP3971_BUCK1 -> 0
  39. LP3971_BUCK2 -> 4
  40. LP3971_BUCK3 -> 6
  41. */
  42. #define BUCK_VOL_CHANGE_SHIFT(x) (((!!x) << 2) | (x & ~0x01))
  43. #define BUCK_VOL_CHANGE_FLAG_GO 0x01
  44. #define BUCK_VOL_CHANGE_FLAG_TARGET 0x02
  45. #define BUCK_VOL_CHANGE_FLAG_MASK 0x03
  46. #define LP3971_BUCK1_BASE 0x23
  47. #define LP3971_BUCK2_BASE 0x29
  48. #define LP3971_BUCK3_BASE 0x32
  49. static const int buck_base_addr[] = {
  50. LP3971_BUCK1_BASE,
  51. LP3971_BUCK2_BASE,
  52. LP3971_BUCK3_BASE,
  53. };
  54. #define LP3971_BUCK_TARGET_VOL1_REG(x) (buck_base_addr[x])
  55. #define LP3971_BUCK_TARGET_VOL2_REG(x) (buck_base_addr[x]+1)
  56. static const int buck_voltage_map[] = {
  57. 0, 800, 850, 900, 950, 1000, 1050, 1100,
  58. 1150, 1200, 1250, 1300, 1350, 1400, 1450, 1500,
  59. 1550, 1600, 1650, 1700, 1800, 1900, 2500, 2800,
  60. 3000, 3300,
  61. };
  62. #define BUCK_TARGET_VOL_MASK 0x3f
  63. #define BUCK_TARGET_VOL_MIN_IDX 0x01
  64. #define BUCK_TARGET_VOL_MAX_IDX 0x19
  65. #define LP3971_BUCK_RAMP_REG(x) (buck_base_addr[x]+2)
  66. #define LP3971_LDO_ENABLE_REG 0x12
  67. #define LP3971_LDO_VOL_CONTR_BASE 0x39
  68. /* Voltage control registers:
  69. LP3971_LDO1 -> LP3971_LDO_VOL_CONTR_BASE + 0
  70. LP3971_LDO2 -> LP3971_LDO_VOL_CONTR_BASE + 0
  71. LP3971_LDO3 -> LP3971_LDO_VOL_CONTR_BASE + 1
  72. LP3971_LDO4 -> LP3971_LDO_VOL_CONTR_BASE + 1
  73. LP3971_LDO5 -> LP3971_LDO_VOL_CONTR_BASE + 2
  74. */
  75. #define LP3971_LDO_VOL_CONTR_REG(x) (LP3971_LDO_VOL_CONTR_BASE + (x >> 1))
  76. /* Voltage control registers shift:
  77. LP3971_LDO1 -> 0, LP3971_LDO2 -> 4
  78. LP3971_LDO3 -> 0, LP3971_LDO4 -> 4
  79. LP3971_LDO5 -> 0
  80. */
  81. #define LDO_VOL_CONTR_SHIFT(x) ((x & 1) << 2)
  82. #define LDO_VOL_CONTR_MASK 0x0f
  83. static const int ldo45_voltage_map[] = {
  84. 1000, 1050, 1100, 1150, 1200, 1250, 1300, 1350,
  85. 1400, 1500, 1800, 1900, 2500, 2800, 3000, 3300,
  86. };
  87. static const int ldo123_voltage_map[] = {
  88. 1800, 1900, 2000, 2100, 2200, 2300, 2400, 2500,
  89. 2600, 2700, 2800, 2900, 3000, 3100, 3200, 3300,
  90. };
  91. static const int *ldo_voltage_map[] = {
  92. ldo123_voltage_map, /* LDO1 */
  93. ldo123_voltage_map, /* LDO2 */
  94. ldo123_voltage_map, /* LDO3 */
  95. ldo45_voltage_map, /* LDO4 */
  96. ldo45_voltage_map, /* LDO5 */
  97. };
  98. #define LDO_VOL_VALUE_MAP(x) (ldo_voltage_map[(x - LP3971_LDO1)])
  99. #define LDO_VOL_MIN_IDX 0x00
  100. #define LDO_VOL_MAX_IDX 0x0f
  101. static int lp3971_ldo_list_voltage(struct regulator_dev *dev, unsigned index)
  102. {
  103. int ldo = rdev_get_id(dev) - LP3971_LDO1;
  104. return 1000 * LDO_VOL_VALUE_MAP(ldo)[index];
  105. }
  106. static int lp3971_ldo_is_enabled(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. u16 val;
  112. val = lp3971_reg_read(lp3971, LP3971_LDO_ENABLE_REG);
  113. return (val & mask) != 0;
  114. }
  115. static int lp3971_ldo_enable(struct regulator_dev *dev)
  116. {
  117. struct lp3971 *lp3971 = rdev_get_drvdata(dev);
  118. int ldo = rdev_get_id(dev) - LP3971_LDO1;
  119. u16 mask = 1 << (1 + ldo);
  120. return lp3971_set_bits(lp3971, LP3971_LDO_ENABLE_REG, mask, mask);
  121. }
  122. static int lp3971_ldo_disable(struct regulator_dev *dev)
  123. {
  124. struct lp3971 *lp3971 = rdev_get_drvdata(dev);
  125. int ldo = rdev_get_id(dev) - LP3971_LDO1;
  126. u16 mask = 1 << (1 + ldo);
  127. return lp3971_set_bits(lp3971, LP3971_LDO_ENABLE_REG, mask, 0);
  128. }
  129. static int lp3971_ldo_get_voltage(struct regulator_dev *dev)
  130. {
  131. struct lp3971 *lp3971 = rdev_get_drvdata(dev);
  132. int ldo = rdev_get_id(dev) - LP3971_LDO1;
  133. u16 val, reg;
  134. reg = lp3971_reg_read(lp3971, LP3971_LDO_VOL_CONTR_REG(ldo));
  135. val = (reg >> LDO_VOL_CONTR_SHIFT(ldo)) & LDO_VOL_CONTR_MASK;
  136. return 1000 * LDO_VOL_VALUE_MAP(ldo)[val];
  137. }
  138. static int lp3971_ldo_set_voltage(struct regulator_dev *dev,
  139. int min_uV, int max_uV,
  140. unsigned int *selector)
  141. {
  142. struct lp3971 *lp3971 = rdev_get_drvdata(dev);
  143. int ldo = rdev_get_id(dev) - LP3971_LDO1;
  144. int min_vol = min_uV / 1000, max_vol = max_uV / 1000;
  145. const int *vol_map = LDO_VOL_VALUE_MAP(ldo);
  146. u16 val;
  147. if (min_vol < vol_map[LDO_VOL_MIN_IDX] ||
  148. min_vol > vol_map[LDO_VOL_MAX_IDX])
  149. return -EINVAL;
  150. for (val = LDO_VOL_MIN_IDX; val <= LDO_VOL_MAX_IDX; val++)
  151. if (vol_map[val] >= min_vol)
  152. break;
  153. if (val > LDO_VOL_MAX_IDX || vol_map[val] > max_vol)
  154. return -EINVAL;
  155. *selector = val;
  156. return lp3971_set_bits(lp3971, LP3971_LDO_VOL_CONTR_REG(ldo),
  157. LDO_VOL_CONTR_MASK << LDO_VOL_CONTR_SHIFT(ldo),
  158. val << LDO_VOL_CONTR_SHIFT(ldo));
  159. }
  160. static struct regulator_ops lp3971_ldo_ops = {
  161. .list_voltage = lp3971_ldo_list_voltage,
  162. .is_enabled = lp3971_ldo_is_enabled,
  163. .enable = lp3971_ldo_enable,
  164. .disable = lp3971_ldo_disable,
  165. .get_voltage = lp3971_ldo_get_voltage,
  166. .set_voltage = lp3971_ldo_set_voltage,
  167. };
  168. static int lp3971_dcdc_list_voltage(struct regulator_dev *dev, unsigned index)
  169. {
  170. return 1000 * buck_voltage_map[index];
  171. }
  172. static int lp3971_dcdc_is_enabled(struct regulator_dev *dev)
  173. {
  174. struct lp3971 *lp3971 = rdev_get_drvdata(dev);
  175. int buck = rdev_get_id(dev) - LP3971_DCDC1;
  176. u16 mask = 1 << (buck * 2);
  177. u16 val;
  178. val = lp3971_reg_read(lp3971, LP3971_BUCK_VOL_ENABLE_REG);
  179. return (val & mask) != 0;
  180. }
  181. static int lp3971_dcdc_enable(struct regulator_dev *dev)
  182. {
  183. struct lp3971 *lp3971 = rdev_get_drvdata(dev);
  184. int buck = rdev_get_id(dev) - LP3971_DCDC1;
  185. u16 mask = 1 << (buck * 2);
  186. return lp3971_set_bits(lp3971, LP3971_BUCK_VOL_ENABLE_REG, mask, mask);
  187. }
  188. static int lp3971_dcdc_disable(struct regulator_dev *dev)
  189. {
  190. struct lp3971 *lp3971 = rdev_get_drvdata(dev);
  191. int buck = rdev_get_id(dev) - LP3971_DCDC1;
  192. u16 mask = 1 << (buck * 2);
  193. return lp3971_set_bits(lp3971, LP3971_BUCK_VOL_ENABLE_REG, mask, 0);
  194. }
  195. static int lp3971_dcdc_get_voltage(struct regulator_dev *dev)
  196. {
  197. struct lp3971 *lp3971 = rdev_get_drvdata(dev);
  198. int buck = rdev_get_id(dev) - LP3971_DCDC1;
  199. u16 reg;
  200. int val;
  201. reg = lp3971_reg_read(lp3971, LP3971_BUCK_TARGET_VOL1_REG(buck));
  202. reg &= BUCK_TARGET_VOL_MASK;
  203. if (reg <= BUCK_TARGET_VOL_MAX_IDX)
  204. val = 1000 * buck_voltage_map[reg];
  205. else {
  206. val = 0;
  207. dev_warn(&dev->dev, "chip reported incorrect voltage value.\n");
  208. }
  209. return val;
  210. }
  211. static int lp3971_dcdc_set_voltage(struct regulator_dev *dev,
  212. int min_uV, int max_uV,
  213. unsigned int *selector)
  214. {
  215. struct lp3971 *lp3971 = rdev_get_drvdata(dev);
  216. int buck = rdev_get_id(dev) - LP3971_DCDC1;
  217. int min_vol = min_uV / 1000, max_vol = max_uV / 1000;
  218. const int *vol_map = buck_voltage_map;
  219. u16 val;
  220. int ret;
  221. if (min_vol < vol_map[BUCK_TARGET_VOL_MIN_IDX] ||
  222. min_vol > vol_map[BUCK_TARGET_VOL_MAX_IDX])
  223. return -EINVAL;
  224. for (val = BUCK_TARGET_VOL_MIN_IDX; val <= BUCK_TARGET_VOL_MAX_IDX;
  225. val++)
  226. if (vol_map[val] >= min_vol)
  227. break;
  228. if (val > BUCK_TARGET_VOL_MAX_IDX || vol_map[val] > max_vol)
  229. return -EINVAL;
  230. *selector = val;
  231. ret = lp3971_set_bits(lp3971, LP3971_BUCK_TARGET_VOL1_REG(buck),
  232. BUCK_TARGET_VOL_MASK, val);
  233. if (ret)
  234. return ret;
  235. ret = lp3971_set_bits(lp3971, LP3971_BUCK_VOL_CHANGE_REG,
  236. BUCK_VOL_CHANGE_FLAG_MASK << BUCK_VOL_CHANGE_SHIFT(buck),
  237. BUCK_VOL_CHANGE_FLAG_GO << BUCK_VOL_CHANGE_SHIFT(buck));
  238. if (ret)
  239. return ret;
  240. return lp3971_set_bits(lp3971, LP3971_BUCK_VOL_CHANGE_REG,
  241. BUCK_VOL_CHANGE_FLAG_MASK << BUCK_VOL_CHANGE_SHIFT(buck),
  242. 0 << BUCK_VOL_CHANGE_SHIFT(buck));
  243. }
  244. static struct regulator_ops lp3971_dcdc_ops = {
  245. .list_voltage = lp3971_dcdc_list_voltage,
  246. .is_enabled = lp3971_dcdc_is_enabled,
  247. .enable = lp3971_dcdc_enable,
  248. .disable = lp3971_dcdc_disable,
  249. .get_voltage = lp3971_dcdc_get_voltage,
  250. .set_voltage = lp3971_dcdc_set_voltage,
  251. };
  252. static struct regulator_desc regulators[] = {
  253. {
  254. .name = "LDO1",
  255. .id = LP3971_LDO1,
  256. .ops = &lp3971_ldo_ops,
  257. .n_voltages = ARRAY_SIZE(ldo123_voltage_map),
  258. .type = REGULATOR_VOLTAGE,
  259. .owner = THIS_MODULE,
  260. },
  261. {
  262. .name = "LDO2",
  263. .id = LP3971_LDO2,
  264. .ops = &lp3971_ldo_ops,
  265. .n_voltages = ARRAY_SIZE(ldo123_voltage_map),
  266. .type = REGULATOR_VOLTAGE,
  267. .owner = THIS_MODULE,
  268. },
  269. {
  270. .name = "LDO3",
  271. .id = LP3971_LDO3,
  272. .ops = &lp3971_ldo_ops,
  273. .n_voltages = ARRAY_SIZE(ldo123_voltage_map),
  274. .type = REGULATOR_VOLTAGE,
  275. .owner = THIS_MODULE,
  276. },
  277. {
  278. .name = "LDO4",
  279. .id = LP3971_LDO4,
  280. .ops = &lp3971_ldo_ops,
  281. .n_voltages = ARRAY_SIZE(ldo45_voltage_map),
  282. .type = REGULATOR_VOLTAGE,
  283. .owner = THIS_MODULE,
  284. },
  285. {
  286. .name = "LDO5",
  287. .id = LP3971_LDO5,
  288. .ops = &lp3971_ldo_ops,
  289. .n_voltages = ARRAY_SIZE(ldo45_voltage_map),
  290. .type = REGULATOR_VOLTAGE,
  291. .owner = THIS_MODULE,
  292. },
  293. {
  294. .name = "DCDC1",
  295. .id = LP3971_DCDC1,
  296. .ops = &lp3971_dcdc_ops,
  297. .n_voltages = ARRAY_SIZE(buck_voltage_map),
  298. .type = REGULATOR_VOLTAGE,
  299. .owner = THIS_MODULE,
  300. },
  301. {
  302. .name = "DCDC2",
  303. .id = LP3971_DCDC2,
  304. .ops = &lp3971_dcdc_ops,
  305. .n_voltages = ARRAY_SIZE(buck_voltage_map),
  306. .type = REGULATOR_VOLTAGE,
  307. .owner = THIS_MODULE,
  308. },
  309. {
  310. .name = "DCDC3",
  311. .id = LP3971_DCDC3,
  312. .ops = &lp3971_dcdc_ops,
  313. .n_voltages = ARRAY_SIZE(buck_voltage_map),
  314. .type = REGULATOR_VOLTAGE,
  315. .owner = THIS_MODULE,
  316. },
  317. };
  318. static int lp3971_i2c_read(struct i2c_client *i2c, char reg, int count,
  319. u16 *dest)
  320. {
  321. int ret;
  322. if (count != 1)
  323. return -EIO;
  324. ret = i2c_smbus_read_byte_data(i2c, reg);
  325. if (ret < 0)
  326. return -EIO;
  327. *dest = ret;
  328. return 0;
  329. }
  330. static int lp3971_i2c_write(struct i2c_client *i2c, char reg, int count,
  331. const u16 *src)
  332. {
  333. if (count != 1)
  334. return -EIO;
  335. return i2c_smbus_write_byte_data(i2c, reg, *src);
  336. }
  337. static u8 lp3971_reg_read(struct lp3971 *lp3971, u8 reg)
  338. {
  339. u16 val = 0;
  340. mutex_lock(&lp3971->io_lock);
  341. lp3971_i2c_read(lp3971->i2c, reg, 1, &val);
  342. dev_dbg(lp3971->dev, "reg read 0x%02x -> 0x%02x\n", (int)reg,
  343. (unsigned)val&0xff);
  344. mutex_unlock(&lp3971->io_lock);
  345. return val & 0xff;
  346. }
  347. static int lp3971_set_bits(struct lp3971 *lp3971, u8 reg, u16 mask, u16 val)
  348. {
  349. u16 tmp;
  350. int ret;
  351. mutex_lock(&lp3971->io_lock);
  352. ret = lp3971_i2c_read(lp3971->i2c, reg, 1, &tmp);
  353. tmp = (tmp & ~mask) | val;
  354. if (ret == 0) {
  355. ret = lp3971_i2c_write(lp3971->i2c, reg, 1, &tmp);
  356. dev_dbg(lp3971->dev, "reg write 0x%02x -> 0x%02x\n", (int)reg,
  357. (unsigned)val&0xff);
  358. }
  359. mutex_unlock(&lp3971->io_lock);
  360. return ret;
  361. }
  362. static int __devinit setup_regulators(struct lp3971 *lp3971,
  363. struct lp3971_platform_data *pdata)
  364. {
  365. int i, err;
  366. lp3971->num_regulators = pdata->num_regulators;
  367. lp3971->rdev = kcalloc(pdata->num_regulators,
  368. sizeof(struct regulator_dev *), GFP_KERNEL);
  369. if (!lp3971->rdev) {
  370. err = -ENOMEM;
  371. goto err_nomem;
  372. }
  373. /* Instantiate the regulators */
  374. for (i = 0; i < pdata->num_regulators; i++) {
  375. struct lp3971_regulator_subdev *reg = &pdata->regulators[i];
  376. lp3971->rdev[i] = regulator_register(&regulators[reg->id],
  377. lp3971->dev, reg->initdata, lp3971);
  378. if (IS_ERR(lp3971->rdev[i])) {
  379. err = PTR_ERR(lp3971->rdev[i]);
  380. dev_err(lp3971->dev, "regulator init failed: %d\n",
  381. err);
  382. goto error;
  383. }
  384. }
  385. return 0;
  386. error:
  387. while (--i >= 0)
  388. regulator_unregister(lp3971->rdev[i]);
  389. kfree(lp3971->rdev);
  390. lp3971->rdev = NULL;
  391. err_nomem:
  392. return err;
  393. }
  394. static int __devinit lp3971_i2c_probe(struct i2c_client *i2c,
  395. const struct i2c_device_id *id)
  396. {
  397. struct lp3971 *lp3971;
  398. struct lp3971_platform_data *pdata = i2c->dev.platform_data;
  399. int ret;
  400. u16 val;
  401. if (!pdata) {
  402. dev_dbg(&i2c->dev, "No platform init data supplied\n");
  403. return -ENODEV;
  404. }
  405. lp3971 = kzalloc(sizeof(struct lp3971), GFP_KERNEL);
  406. if (lp3971 == NULL)
  407. return -ENOMEM;
  408. lp3971->i2c = i2c;
  409. lp3971->dev = &i2c->dev;
  410. mutex_init(&lp3971->io_lock);
  411. /* Detect LP3971 */
  412. ret = lp3971_i2c_read(i2c, LP3971_SYS_CONTROL1_REG, 1, &val);
  413. if (ret == 0 && (val & SYS_CONTROL1_INIT_MASK) != SYS_CONTROL1_INIT_VAL)
  414. ret = -ENODEV;
  415. if (ret < 0) {
  416. dev_err(&i2c->dev, "failed to detect device\n");
  417. goto err_detect;
  418. }
  419. ret = setup_regulators(lp3971, pdata);
  420. if (ret < 0)
  421. goto err_detect;
  422. i2c_set_clientdata(i2c, lp3971);
  423. return 0;
  424. err_detect:
  425. kfree(lp3971);
  426. return ret;
  427. }
  428. static int __devexit lp3971_i2c_remove(struct i2c_client *i2c)
  429. {
  430. struct lp3971 *lp3971 = i2c_get_clientdata(i2c);
  431. int i;
  432. for (i = 0; i < lp3971->num_regulators; i++)
  433. regulator_unregister(lp3971->rdev[i]);
  434. kfree(lp3971->rdev);
  435. kfree(lp3971);
  436. return 0;
  437. }
  438. static const struct i2c_device_id lp3971_i2c_id[] = {
  439. { "lp3971", 0 },
  440. { }
  441. };
  442. MODULE_DEVICE_TABLE(i2c, lp3971_i2c_id);
  443. static struct i2c_driver lp3971_i2c_driver = {
  444. .driver = {
  445. .name = "LP3971",
  446. .owner = THIS_MODULE,
  447. },
  448. .probe = lp3971_i2c_probe,
  449. .remove = __devexit_p(lp3971_i2c_remove),
  450. .id_table = lp3971_i2c_id,
  451. };
  452. static int __init lp3971_module_init(void)
  453. {
  454. int ret;
  455. ret = i2c_add_driver(&lp3971_i2c_driver);
  456. if (ret != 0)
  457. pr_err("Failed to register I2C driver: %d\n", ret);
  458. return ret;
  459. }
  460. module_init(lp3971_module_init);
  461. static void __exit lp3971_module_exit(void)
  462. {
  463. i2c_del_driver(&lp3971_i2c_driver);
  464. }
  465. module_exit(lp3971_module_exit);
  466. MODULE_LICENSE("GPL");
  467. MODULE_AUTHOR("Marek Szyprowski <m.szyprowski@samsung.com>");
  468. MODULE_DESCRIPTION("LP3971 PMIC driver");