lp3971.c 14 KB

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