tps65023-regulator.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  1. /*
  2. * tps65023-regulator.c
  3. *
  4. * Supports TPS65023 Regulator
  5. *
  6. * Copyright (C) 2009 Texas Instrument Incorporated - http://www.ti.com/
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation version 2.
  11. *
  12. * This program is distributed "as is" WITHOUT ANY WARRANTY of any kind,
  13. * whether express or implied; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. */
  17. #include <linux/kernel.h>
  18. #include <linux/module.h>
  19. #include <linux/init.h>
  20. #include <linux/err.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/regulator/driver.h>
  23. #include <linux/regulator/machine.h>
  24. #include <linux/i2c.h>
  25. #include <linux/delay.h>
  26. #include <linux/slab.h>
  27. #include <linux/regmap.h>
  28. /* Register definitions */
  29. #define TPS65023_REG_VERSION 0
  30. #define TPS65023_REG_PGOODZ 1
  31. #define TPS65023_REG_MASK 2
  32. #define TPS65023_REG_REG_CTRL 3
  33. #define TPS65023_REG_CON_CTRL 4
  34. #define TPS65023_REG_CON_CTRL2 5
  35. #define TPS65023_REG_DEF_CORE 6
  36. #define TPS65023_REG_DEFSLEW 7
  37. #define TPS65023_REG_LDO_CTRL 8
  38. /* PGOODZ bitfields */
  39. #define TPS65023_PGOODZ_PWRFAILZ BIT(7)
  40. #define TPS65023_PGOODZ_LOWBATTZ BIT(6)
  41. #define TPS65023_PGOODZ_VDCDC1 BIT(5)
  42. #define TPS65023_PGOODZ_VDCDC2 BIT(4)
  43. #define TPS65023_PGOODZ_VDCDC3 BIT(3)
  44. #define TPS65023_PGOODZ_LDO2 BIT(2)
  45. #define TPS65023_PGOODZ_LDO1 BIT(1)
  46. /* MASK bitfields */
  47. #define TPS65023_MASK_PWRFAILZ BIT(7)
  48. #define TPS65023_MASK_LOWBATTZ BIT(6)
  49. #define TPS65023_MASK_VDCDC1 BIT(5)
  50. #define TPS65023_MASK_VDCDC2 BIT(4)
  51. #define TPS65023_MASK_VDCDC3 BIT(3)
  52. #define TPS65023_MASK_LDO2 BIT(2)
  53. #define TPS65023_MASK_LDO1 BIT(1)
  54. /* REG_CTRL bitfields */
  55. #define TPS65023_REG_CTRL_VDCDC1_EN BIT(5)
  56. #define TPS65023_REG_CTRL_VDCDC2_EN BIT(4)
  57. #define TPS65023_REG_CTRL_VDCDC3_EN BIT(3)
  58. #define TPS65023_REG_CTRL_LDO2_EN BIT(2)
  59. #define TPS65023_REG_CTRL_LDO1_EN BIT(1)
  60. /* LDO_CTRL bitfields */
  61. #define TPS65023_LDO_CTRL_LDOx_SHIFT(ldo_id) ((ldo_id)*4)
  62. #define TPS65023_LDO_CTRL_LDOx_MASK(ldo_id) (0xF0 >> ((ldo_id)*4))
  63. /* Number of step-down converters available */
  64. #define TPS65023_NUM_DCDC 3
  65. /* Number of LDO voltage regulators available */
  66. #define TPS65023_NUM_LDO 2
  67. /* Number of total regulators available */
  68. #define TPS65023_NUM_REGULATOR (TPS65023_NUM_DCDC + TPS65023_NUM_LDO)
  69. /* DCDCs */
  70. #define TPS65023_DCDC_1 0
  71. #define TPS65023_DCDC_2 1
  72. #define TPS65023_DCDC_3 2
  73. /* LDOs */
  74. #define TPS65023_LDO_1 3
  75. #define TPS65023_LDO_2 4
  76. #define TPS65023_MAX_REG_ID TPS65023_LDO_2
  77. /* Supported voltage values for regulators */
  78. static const u16 VDCDC1_VSEL_table[] = {
  79. 800, 825, 850, 875,
  80. 900, 925, 950, 975,
  81. 1000, 1025, 1050, 1075,
  82. 1100, 1125, 1150, 1175,
  83. 1200, 1225, 1250, 1275,
  84. 1300, 1325, 1350, 1375,
  85. 1400, 1425, 1450, 1475,
  86. 1500, 1525, 1550, 1600,
  87. };
  88. static const u16 LDO1_VSEL_table[] = {
  89. 1000, 1100, 1300, 1800,
  90. 2200, 2600, 2800, 3150,
  91. };
  92. static const u16 LDO2_VSEL_table[] = {
  93. 1050, 1200, 1300, 1800,
  94. 2500, 2800, 3000, 3300,
  95. };
  96. static unsigned int num_voltages[] = {ARRAY_SIZE(VDCDC1_VSEL_table),
  97. 0, 0, ARRAY_SIZE(LDO1_VSEL_table),
  98. ARRAY_SIZE(LDO2_VSEL_table)};
  99. /* Regulator specific details */
  100. struct tps_info {
  101. const char *name;
  102. unsigned min_uV;
  103. unsigned max_uV;
  104. bool fixed;
  105. u8 table_len;
  106. const u16 *table;
  107. };
  108. /* PMIC details */
  109. struct tps_pmic {
  110. struct regulator_desc desc[TPS65023_NUM_REGULATOR];
  111. struct i2c_client *client;
  112. struct regulator_dev *rdev[TPS65023_NUM_REGULATOR];
  113. const struct tps_info *info[TPS65023_NUM_REGULATOR];
  114. struct regmap *regmap;
  115. };
  116. static int tps_65023_set_bits(struct tps_pmic *tps, u8 reg, u8 mask)
  117. {
  118. return regmap_update_bits(tps->regmap, reg, mask, mask);
  119. }
  120. static int tps_65023_clear_bits(struct tps_pmic *tps, u8 reg, u8 mask)
  121. {
  122. return regmap_update_bits(tps->regmap, reg, mask, 0);
  123. }
  124. static int tps_65023_reg_read(struct tps_pmic *tps, u8 reg)
  125. {
  126. unsigned int val;
  127. int ret;
  128. ret = regmap_read(tps->regmap, reg, &val);
  129. if (ret != 0)
  130. return ret;
  131. else
  132. return val;
  133. }
  134. static int tps_65023_reg_write(struct tps_pmic *tps, u8 reg, u8 val)
  135. {
  136. return regmap_write(tps->regmap, reg, val);
  137. }
  138. static int tps65023_dcdc_is_enabled(struct regulator_dev *dev)
  139. {
  140. struct tps_pmic *tps = rdev_get_drvdata(dev);
  141. int data, dcdc = rdev_get_id(dev);
  142. u8 shift;
  143. if (dcdc < TPS65023_DCDC_1 || dcdc > TPS65023_DCDC_3)
  144. return -EINVAL;
  145. shift = TPS65023_NUM_REGULATOR - dcdc;
  146. data = tps_65023_reg_read(tps, TPS65023_REG_REG_CTRL);
  147. if (data < 0)
  148. return data;
  149. else
  150. return (data & 1<<shift) ? 1 : 0;
  151. }
  152. static int tps65023_ldo_is_enabled(struct regulator_dev *dev)
  153. {
  154. struct tps_pmic *tps = rdev_get_drvdata(dev);
  155. int data, ldo = rdev_get_id(dev);
  156. u8 shift;
  157. if (ldo < TPS65023_LDO_1 || ldo > TPS65023_LDO_2)
  158. return -EINVAL;
  159. shift = (ldo == TPS65023_LDO_1 ? 1 : 2);
  160. data = tps_65023_reg_read(tps, TPS65023_REG_REG_CTRL);
  161. if (data < 0)
  162. return data;
  163. else
  164. return (data & 1<<shift) ? 1 : 0;
  165. }
  166. static int tps65023_dcdc_enable(struct regulator_dev *dev)
  167. {
  168. struct tps_pmic *tps = rdev_get_drvdata(dev);
  169. int dcdc = rdev_get_id(dev);
  170. u8 shift;
  171. if (dcdc < TPS65023_DCDC_1 || dcdc > TPS65023_DCDC_3)
  172. return -EINVAL;
  173. shift = TPS65023_NUM_REGULATOR - dcdc;
  174. return tps_65023_set_bits(tps, TPS65023_REG_REG_CTRL, 1 << shift);
  175. }
  176. static int tps65023_dcdc_disable(struct regulator_dev *dev)
  177. {
  178. struct tps_pmic *tps = rdev_get_drvdata(dev);
  179. int dcdc = rdev_get_id(dev);
  180. u8 shift;
  181. if (dcdc < TPS65023_DCDC_1 || dcdc > TPS65023_DCDC_3)
  182. return -EINVAL;
  183. shift = TPS65023_NUM_REGULATOR - dcdc;
  184. return tps_65023_clear_bits(tps, TPS65023_REG_REG_CTRL, 1 << shift);
  185. }
  186. static int tps65023_ldo_enable(struct regulator_dev *dev)
  187. {
  188. struct tps_pmic *tps = rdev_get_drvdata(dev);
  189. int ldo = rdev_get_id(dev);
  190. u8 shift;
  191. if (ldo < TPS65023_LDO_1 || ldo > TPS65023_LDO_2)
  192. return -EINVAL;
  193. shift = (ldo == TPS65023_LDO_1 ? 1 : 2);
  194. return tps_65023_set_bits(tps, TPS65023_REG_REG_CTRL, 1 << shift);
  195. }
  196. static int tps65023_ldo_disable(struct regulator_dev *dev)
  197. {
  198. struct tps_pmic *tps = rdev_get_drvdata(dev);
  199. int ldo = rdev_get_id(dev);
  200. u8 shift;
  201. if (ldo < TPS65023_LDO_1 || ldo > TPS65023_LDO_2)
  202. return -EINVAL;
  203. shift = (ldo == TPS65023_LDO_1 ? 1 : 2);
  204. return tps_65023_clear_bits(tps, TPS65023_REG_REG_CTRL, 1 << shift);
  205. }
  206. static int tps65023_dcdc_get_voltage(struct regulator_dev *dev)
  207. {
  208. struct tps_pmic *tps = rdev_get_drvdata(dev);
  209. int data, dcdc = rdev_get_id(dev);
  210. if (dcdc < TPS65023_DCDC_1 || dcdc > TPS65023_DCDC_3)
  211. return -EINVAL;
  212. if (dcdc == TPS65023_DCDC_1) {
  213. data = tps_65023_reg_read(tps, TPS65023_REG_DEF_CORE);
  214. if (data < 0)
  215. return data;
  216. data &= (tps->info[dcdc]->table_len - 1);
  217. return tps->info[dcdc]->table[data] * 1000;
  218. } else
  219. return tps->info[dcdc]->min_uV;
  220. }
  221. static int tps65023_dcdc_set_voltage(struct regulator_dev *dev,
  222. int min_uV, int max_uV,
  223. unsigned *selector)
  224. {
  225. struct tps_pmic *tps = rdev_get_drvdata(dev);
  226. int dcdc = rdev_get_id(dev);
  227. int vsel;
  228. if (dcdc != TPS65023_DCDC_1)
  229. return -EINVAL;
  230. if (min_uV < tps->info[dcdc]->min_uV
  231. || min_uV > tps->info[dcdc]->max_uV)
  232. return -EINVAL;
  233. if (max_uV < tps->info[dcdc]->min_uV
  234. || max_uV > tps->info[dcdc]->max_uV)
  235. return -EINVAL;
  236. for (vsel = 0; vsel < tps->info[dcdc]->table_len; vsel++) {
  237. int mV = tps->info[dcdc]->table[vsel];
  238. int uV = mV * 1000;
  239. /* Break at the first in-range value */
  240. if (min_uV <= uV && uV <= max_uV)
  241. break;
  242. }
  243. *selector = vsel;
  244. /* write to the register in case we found a match */
  245. if (vsel == tps->info[dcdc]->table_len)
  246. return -EINVAL;
  247. else
  248. return tps_65023_reg_write(tps, TPS65023_REG_DEF_CORE, vsel);
  249. }
  250. static int tps65023_ldo_get_voltage(struct regulator_dev *dev)
  251. {
  252. struct tps_pmic *tps = rdev_get_drvdata(dev);
  253. int data, ldo = rdev_get_id(dev);
  254. if (ldo < TPS65023_LDO_1 || ldo > TPS65023_LDO_2)
  255. return -EINVAL;
  256. data = tps_65023_reg_read(tps, TPS65023_REG_LDO_CTRL);
  257. if (data < 0)
  258. return data;
  259. data >>= (TPS65023_LDO_CTRL_LDOx_SHIFT(ldo - TPS65023_LDO_1));
  260. data &= (tps->info[ldo]->table_len - 1);
  261. return tps->info[ldo]->table[data] * 1000;
  262. }
  263. static int tps65023_ldo_set_voltage(struct regulator_dev *dev,
  264. int min_uV, int max_uV, unsigned *selector)
  265. {
  266. struct tps_pmic *tps = rdev_get_drvdata(dev);
  267. int data, vsel, ldo = rdev_get_id(dev);
  268. if (ldo < TPS65023_LDO_1 || ldo > TPS65023_LDO_2)
  269. return -EINVAL;
  270. if (min_uV < tps->info[ldo]->min_uV || min_uV > tps->info[ldo]->max_uV)
  271. return -EINVAL;
  272. if (max_uV < tps->info[ldo]->min_uV || max_uV > tps->info[ldo]->max_uV)
  273. return -EINVAL;
  274. for (vsel = 0; vsel < tps->info[ldo]->table_len; vsel++) {
  275. int mV = tps->info[ldo]->table[vsel];
  276. int uV = mV * 1000;
  277. /* Break at the first in-range value */
  278. if (min_uV <= uV && uV <= max_uV)
  279. break;
  280. }
  281. if (vsel == tps->info[ldo]->table_len)
  282. return -EINVAL;
  283. *selector = vsel;
  284. data = tps_65023_reg_read(tps, TPS65023_REG_LDO_CTRL);
  285. if (data < 0)
  286. return data;
  287. data &= TPS65023_LDO_CTRL_LDOx_MASK(ldo - TPS65023_LDO_1);
  288. data |= (vsel << (TPS65023_LDO_CTRL_LDOx_SHIFT(ldo - TPS65023_LDO_1)));
  289. return tps_65023_reg_write(tps, TPS65023_REG_LDO_CTRL, data);
  290. }
  291. static int tps65023_dcdc_list_voltage(struct regulator_dev *dev,
  292. unsigned selector)
  293. {
  294. struct tps_pmic *tps = rdev_get_drvdata(dev);
  295. int dcdc = rdev_get_id(dev);
  296. if (dcdc < TPS65023_DCDC_1 || dcdc > TPS65023_DCDC_3)
  297. return -EINVAL;
  298. if (dcdc == TPS65023_DCDC_1) {
  299. if (selector >= tps->info[dcdc]->table_len)
  300. return -EINVAL;
  301. else
  302. return tps->info[dcdc]->table[selector] * 1000;
  303. } else
  304. return tps->info[dcdc]->min_uV;
  305. }
  306. static int tps65023_ldo_list_voltage(struct regulator_dev *dev,
  307. unsigned selector)
  308. {
  309. struct tps_pmic *tps = rdev_get_drvdata(dev);
  310. int ldo = rdev_get_id(dev);
  311. if (ldo < TPS65023_LDO_1 || ldo > TPS65023_LDO_2)
  312. return -EINVAL;
  313. if (selector >= tps->info[ldo]->table_len)
  314. return -EINVAL;
  315. else
  316. return tps->info[ldo]->table[selector] * 1000;
  317. }
  318. /* Operations permitted on VDCDCx */
  319. static struct regulator_ops tps65023_dcdc_ops = {
  320. .is_enabled = tps65023_dcdc_is_enabled,
  321. .enable = tps65023_dcdc_enable,
  322. .disable = tps65023_dcdc_disable,
  323. .get_voltage = tps65023_dcdc_get_voltage,
  324. .set_voltage = tps65023_dcdc_set_voltage,
  325. .list_voltage = tps65023_dcdc_list_voltage,
  326. };
  327. /* Operations permitted on LDOx */
  328. static struct regulator_ops tps65023_ldo_ops = {
  329. .is_enabled = tps65023_ldo_is_enabled,
  330. .enable = tps65023_ldo_enable,
  331. .disable = tps65023_ldo_disable,
  332. .get_voltage = tps65023_ldo_get_voltage,
  333. .set_voltage = tps65023_ldo_set_voltage,
  334. .list_voltage = tps65023_ldo_list_voltage,
  335. };
  336. static struct regmap_config tps65023_regmap_config = {
  337. .reg_bits = 8,
  338. .val_bits = 8,
  339. };
  340. static int __devinit tps_65023_probe(struct i2c_client *client,
  341. const struct i2c_device_id *id)
  342. {
  343. const struct tps_info *info = (void *)id->driver_data;
  344. struct regulator_init_data *init_data;
  345. struct regulator_dev *rdev;
  346. struct tps_pmic *tps;
  347. int i;
  348. int error;
  349. if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  350. return -EIO;
  351. /**
  352. * init_data points to array of regulator_init structures
  353. * coming from the board-evm file.
  354. */
  355. init_data = client->dev.platform_data;
  356. if (!init_data)
  357. return -EIO;
  358. tps = kzalloc(sizeof(*tps), GFP_KERNEL);
  359. if (!tps)
  360. return -ENOMEM;
  361. tps->regmap = regmap_init_i2c(client, &tps65023_regmap_config);
  362. if (IS_ERR(tps->regmap)) {
  363. error = PTR_ERR(tps->regmap);
  364. dev_err(&client->dev, "Failed to allocate register map: %d\n",
  365. error);
  366. goto fail_alloc;
  367. }
  368. /* common for all regulators */
  369. tps->client = client;
  370. for (i = 0; i < TPS65023_NUM_REGULATOR; i++, info++, init_data++) {
  371. /* Store regulator specific information */
  372. tps->info[i] = info;
  373. tps->desc[i].name = info->name;
  374. tps->desc[i].id = i;
  375. tps->desc[i].n_voltages = num_voltages[i];
  376. tps->desc[i].ops = (i > TPS65023_DCDC_3 ?
  377. &tps65023_ldo_ops : &tps65023_dcdc_ops);
  378. tps->desc[i].type = REGULATOR_VOLTAGE;
  379. tps->desc[i].owner = THIS_MODULE;
  380. /* Register the regulators */
  381. rdev = regulator_register(&tps->desc[i], &client->dev,
  382. init_data, tps);
  383. if (IS_ERR(rdev)) {
  384. dev_err(&client->dev, "failed to register %s\n",
  385. id->name);
  386. error = PTR_ERR(rdev);
  387. goto fail;
  388. }
  389. /* Save regulator for cleanup */
  390. tps->rdev[i] = rdev;
  391. }
  392. i2c_set_clientdata(client, tps);
  393. return 0;
  394. fail:
  395. while (--i >= 0)
  396. regulator_unregister(tps->rdev[i]);
  397. regmap_exit(tps->regmap);
  398. fail_alloc:
  399. kfree(tps);
  400. return error;
  401. }
  402. /**
  403. * tps_65023_remove - TPS65023 driver i2c remove handler
  404. * @client: i2c driver client device structure
  405. *
  406. * Unregister TPS driver as an i2c client device driver
  407. */
  408. static int __devexit tps_65023_remove(struct i2c_client *client)
  409. {
  410. struct tps_pmic *tps = i2c_get_clientdata(client);
  411. int i;
  412. for (i = 0; i < TPS65023_NUM_REGULATOR; i++)
  413. regulator_unregister(tps->rdev[i]);
  414. regmap_exit(tps->regmap);
  415. kfree(tps);
  416. return 0;
  417. }
  418. static const struct tps_info tps65023_regs[] = {
  419. {
  420. .name = "VDCDC1",
  421. .min_uV = 800000,
  422. .max_uV = 1600000,
  423. .table_len = ARRAY_SIZE(VDCDC1_VSEL_table),
  424. .table = VDCDC1_VSEL_table,
  425. },
  426. {
  427. .name = "VDCDC2",
  428. .min_uV = 3300000,
  429. .max_uV = 3300000,
  430. .fixed = 1,
  431. },
  432. {
  433. .name = "VDCDC3",
  434. .min_uV = 1800000,
  435. .max_uV = 1800000,
  436. .fixed = 1,
  437. },
  438. {
  439. .name = "LDO1",
  440. .min_uV = 1000000,
  441. .max_uV = 3150000,
  442. .table_len = ARRAY_SIZE(LDO1_VSEL_table),
  443. .table = LDO1_VSEL_table,
  444. },
  445. {
  446. .name = "LDO2",
  447. .min_uV = 1050000,
  448. .max_uV = 3300000,
  449. .table_len = ARRAY_SIZE(LDO2_VSEL_table),
  450. .table = LDO2_VSEL_table,
  451. },
  452. };
  453. static const struct i2c_device_id tps_65023_id[] = {
  454. {.name = "tps65023",
  455. .driver_data = (unsigned long) tps65023_regs,},
  456. {.name = "tps65021",
  457. .driver_data = (unsigned long) tps65023_regs,},
  458. { },
  459. };
  460. MODULE_DEVICE_TABLE(i2c, tps_65023_id);
  461. static struct i2c_driver tps_65023_i2c_driver = {
  462. .driver = {
  463. .name = "tps65023",
  464. .owner = THIS_MODULE,
  465. },
  466. .probe = tps_65023_probe,
  467. .remove = __devexit_p(tps_65023_remove),
  468. .id_table = tps_65023_id,
  469. };
  470. /**
  471. * tps_65023_init
  472. *
  473. * Module init function
  474. */
  475. static int __init tps_65023_init(void)
  476. {
  477. return i2c_add_driver(&tps_65023_i2c_driver);
  478. }
  479. subsys_initcall(tps_65023_init);
  480. /**
  481. * tps_65023_cleanup
  482. *
  483. * Module exit function
  484. */
  485. static void __exit tps_65023_cleanup(void)
  486. {
  487. i2c_del_driver(&tps_65023_i2c_driver);
  488. }
  489. module_exit(tps_65023_cleanup);
  490. MODULE_AUTHOR("Texas Instruments");
  491. MODULE_DESCRIPTION("TPS65023 voltage regulator driver");
  492. MODULE_LICENSE("GPL v2");