tps62360-regulator.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  1. /*
  2. * tps62360.c -- TI tps62360
  3. *
  4. * Driver for processor core supply tps62360, tps62361B, tps62362 and tps62363.
  5. *
  6. * Copyright (c) 2012, NVIDIA Corporation.
  7. *
  8. * Author: Laxman Dewangan <ldewangan@nvidia.com>
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License as
  12. * published by the Free Software Foundation version 2.
  13. *
  14. * This program is distributed "as is" WITHOUT ANY WARRANTY of any kind,
  15. * whether express or implied; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  22. * 02111-1307, USA
  23. */
  24. #include <linux/kernel.h>
  25. #include <linux/module.h>
  26. #include <linux/init.h>
  27. #include <linux/err.h>
  28. #include <linux/of.h>
  29. #include <linux/of_device.h>
  30. #include <linux/of_gpio.h>
  31. #include <linux/regulator/of_regulator.h>
  32. #include <linux/platform_device.h>
  33. #include <linux/regulator/driver.h>
  34. #include <linux/regulator/machine.h>
  35. #include <linux/regulator/tps62360.h>
  36. #include <linux/gpio.h>
  37. #include <linux/i2c.h>
  38. #include <linux/slab.h>
  39. #include <linux/regmap.h>
  40. /* Register definitions */
  41. #define REG_VSET0 0
  42. #define REG_VSET1 1
  43. #define REG_VSET2 2
  44. #define REG_VSET3 3
  45. #define REG_CONTROL 4
  46. #define REG_TEMP 5
  47. #define REG_RAMPCTRL 6
  48. #define REG_CHIPID 8
  49. #define FORCE_PWM_ENABLE BIT(7)
  50. enum chips {TPS62360, TPS62361, TPS62362, TPS62363};
  51. #define TPS62360_BASE_VOLTAGE 770000
  52. #define TPS62360_N_VOLTAGES 64
  53. #define TPS62361_BASE_VOLTAGE 500000
  54. #define TPS62361_N_VOLTAGES 128
  55. /* tps 62360 chip information */
  56. struct tps62360_chip {
  57. struct device *dev;
  58. struct regulator_desc desc;
  59. struct regulator_dev *rdev;
  60. struct regmap *regmap;
  61. int vsel0_gpio;
  62. int vsel1_gpio;
  63. u8 voltage_reg_mask;
  64. bool en_internal_pulldn;
  65. bool en_discharge;
  66. bool valid_gpios;
  67. int lru_index[4];
  68. int curr_vset_vsel[4];
  69. int curr_vset_id;
  70. };
  71. /*
  72. * find_voltage_set_register: Find new voltage configuration register
  73. * (VSET) id.
  74. * The finding of the new VSET register will be based on the LRU mechanism.
  75. * Each VSET register will have different voltage configured . This
  76. * Function will look if any of the VSET register have requested voltage set
  77. * or not.
  78. * - If it is already there then it will make that register as most
  79. * recently used and return as found so that caller need not to set
  80. * the VSET register but need to set the proper gpios to select this
  81. * VSET register.
  82. * - If requested voltage is not found then it will use the least
  83. * recently mechanism to get new VSET register for new configuration
  84. * and will return not_found so that caller need to set new VSET
  85. * register and then gpios (both).
  86. */
  87. static bool find_voltage_set_register(struct tps62360_chip *tps,
  88. int req_vsel, int *vset_reg_id)
  89. {
  90. int i;
  91. bool found = false;
  92. int new_vset_reg = tps->lru_index[3];
  93. int found_index = 3;
  94. for (i = 0; i < 4; ++i) {
  95. if (tps->curr_vset_vsel[tps->lru_index[i]] == req_vsel) {
  96. new_vset_reg = tps->lru_index[i];
  97. found_index = i;
  98. found = true;
  99. goto update_lru_index;
  100. }
  101. }
  102. update_lru_index:
  103. for (i = found_index; i > 0; i--)
  104. tps->lru_index[i] = tps->lru_index[i - 1];
  105. tps->lru_index[0] = new_vset_reg;
  106. *vset_reg_id = new_vset_reg;
  107. return found;
  108. }
  109. static int tps62360_dcdc_get_voltage_sel(struct regulator_dev *dev)
  110. {
  111. struct tps62360_chip *tps = rdev_get_drvdata(dev);
  112. int vsel;
  113. unsigned int data;
  114. int ret;
  115. ret = regmap_read(tps->regmap, REG_VSET0 + tps->curr_vset_id, &data);
  116. if (ret < 0) {
  117. dev_err(tps->dev, "%s(): register %d read failed with err %d\n",
  118. __func__, REG_VSET0 + tps->curr_vset_id, ret);
  119. return ret;
  120. }
  121. vsel = (int)data & tps->voltage_reg_mask;
  122. return vsel;
  123. }
  124. static int tps62360_dcdc_set_voltage_sel(struct regulator_dev *dev,
  125. unsigned selector)
  126. {
  127. struct tps62360_chip *tps = rdev_get_drvdata(dev);
  128. int ret;
  129. bool found = false;
  130. int new_vset_id = tps->curr_vset_id;
  131. /*
  132. * If gpios are available to select the VSET register then least
  133. * recently used register for new configuration.
  134. */
  135. if (tps->valid_gpios)
  136. found = find_voltage_set_register(tps, selector, &new_vset_id);
  137. if (!found) {
  138. ret = regmap_update_bits(tps->regmap, REG_VSET0 + new_vset_id,
  139. tps->voltage_reg_mask, selector);
  140. if (ret < 0) {
  141. dev_err(tps->dev,
  142. "%s(): register %d update failed with err %d\n",
  143. __func__, REG_VSET0 + new_vset_id, ret);
  144. return ret;
  145. }
  146. tps->curr_vset_id = new_vset_id;
  147. tps->curr_vset_vsel[new_vset_id] = selector;
  148. }
  149. /* Select proper VSET register vio gpios */
  150. if (tps->valid_gpios) {
  151. gpio_set_value_cansleep(tps->vsel0_gpio, new_vset_id & 0x1);
  152. gpio_set_value_cansleep(tps->vsel1_gpio,
  153. (new_vset_id >> 1) & 0x1);
  154. }
  155. return 0;
  156. }
  157. static int tps62360_set_mode(struct regulator_dev *rdev, unsigned int mode)
  158. {
  159. struct tps62360_chip *tps = rdev_get_drvdata(rdev);
  160. int i;
  161. int val;
  162. int ret;
  163. /* Enable force PWM mode in FAST mode only. */
  164. switch (mode) {
  165. case REGULATOR_MODE_FAST:
  166. val = FORCE_PWM_ENABLE;
  167. break;
  168. case REGULATOR_MODE_NORMAL:
  169. val = 0;
  170. break;
  171. default:
  172. return -EINVAL;
  173. }
  174. if (!tps->valid_gpios) {
  175. ret = regmap_update_bits(tps->regmap,
  176. REG_VSET0 + tps->curr_vset_id, FORCE_PWM_ENABLE, val);
  177. if (ret < 0)
  178. dev_err(tps->dev,
  179. "%s(): register %d update failed with err %d\n",
  180. __func__, REG_VSET0 + tps->curr_vset_id, ret);
  181. return ret;
  182. }
  183. /* If gpios are valid then all register set need to be control */
  184. for (i = 0; i < 4; ++i) {
  185. ret = regmap_update_bits(tps->regmap,
  186. REG_VSET0 + i, FORCE_PWM_ENABLE, val);
  187. if (ret < 0) {
  188. dev_err(tps->dev,
  189. "%s(): register %d update failed with err %d\n",
  190. __func__, REG_VSET0 + i, ret);
  191. return ret;
  192. }
  193. }
  194. return ret;
  195. }
  196. static unsigned int tps62360_get_mode(struct regulator_dev *rdev)
  197. {
  198. struct tps62360_chip *tps = rdev_get_drvdata(rdev);
  199. unsigned int data;
  200. int ret;
  201. ret = regmap_read(tps->regmap, REG_VSET0 + tps->curr_vset_id, &data);
  202. if (ret < 0) {
  203. dev_err(tps->dev, "%s(): register %d read failed with err %d\n",
  204. __func__, REG_VSET0 + tps->curr_vset_id, ret);
  205. return ret;
  206. }
  207. return (data & FORCE_PWM_ENABLE) ?
  208. REGULATOR_MODE_FAST : REGULATOR_MODE_NORMAL;
  209. }
  210. static const struct regulator_ops tps62360_dcdc_ops = {
  211. .get_voltage_sel = tps62360_dcdc_get_voltage_sel,
  212. .set_voltage_sel = tps62360_dcdc_set_voltage_sel,
  213. .list_voltage = regulator_list_voltage_linear,
  214. .map_voltage = regulator_map_voltage_linear,
  215. .set_voltage_time_sel = regulator_set_voltage_time_sel,
  216. .set_mode = tps62360_set_mode,
  217. .get_mode = tps62360_get_mode,
  218. };
  219. static int tps62360_init_dcdc(struct tps62360_chip *tps,
  220. struct tps62360_regulator_platform_data *pdata)
  221. {
  222. int ret;
  223. unsigned int ramp_ctrl;
  224. /* Initialize internal pull up/down control */
  225. if (tps->en_internal_pulldn)
  226. ret = regmap_write(tps->regmap, REG_CONTROL, 0xE0);
  227. else
  228. ret = regmap_write(tps->regmap, REG_CONTROL, 0x0);
  229. if (ret < 0) {
  230. dev_err(tps->dev,
  231. "%s(): register %d write failed with err %d\n",
  232. __func__, REG_CONTROL, ret);
  233. return ret;
  234. }
  235. /* Reset output discharge path to reduce power consumption */
  236. ret = regmap_update_bits(tps->regmap, REG_RAMPCTRL, BIT(2), 0);
  237. if (ret < 0) {
  238. dev_err(tps->dev,
  239. "%s(): register %d update failed with err %d\n",
  240. __func__, REG_RAMPCTRL, ret);
  241. return ret;
  242. }
  243. /* Get ramp value from ramp control register */
  244. ret = regmap_read(tps->regmap, REG_RAMPCTRL, &ramp_ctrl);
  245. if (ret < 0) {
  246. dev_err(tps->dev,
  247. "%s(): register %d read failed with err %d\n",
  248. __func__, REG_RAMPCTRL, ret);
  249. return ret;
  250. }
  251. ramp_ctrl = (ramp_ctrl >> 5) & 0x7;
  252. /* ramp mV/us = 32/(2^ramp_ctrl) */
  253. tps->desc.ramp_delay = DIV_ROUND_UP(32000, BIT(ramp_ctrl));
  254. return ret;
  255. }
  256. static const struct regmap_config tps62360_regmap_config = {
  257. .reg_bits = 8,
  258. .val_bits = 8,
  259. .max_register = REG_CHIPID,
  260. .cache_type = REGCACHE_RBTREE,
  261. };
  262. static struct tps62360_regulator_platform_data *
  263. of_get_tps62360_platform_data(struct device *dev,
  264. const struct regulator_desc *desc)
  265. {
  266. struct tps62360_regulator_platform_data *pdata;
  267. struct device_node *np = dev->of_node;
  268. pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
  269. if (!pdata)
  270. return NULL;
  271. pdata->reg_init_data = of_get_regulator_init_data(dev, dev->of_node,
  272. desc);
  273. if (!pdata->reg_init_data) {
  274. dev_err(dev, "Not able to get OF regulator init data\n");
  275. return NULL;
  276. }
  277. pdata->vsel0_gpio = of_get_named_gpio(np, "vsel0-gpio", 0);
  278. pdata->vsel1_gpio = of_get_named_gpio(np, "vsel1-gpio", 0);
  279. if (of_find_property(np, "ti,vsel0-state-high", NULL))
  280. pdata->vsel0_def_state = 1;
  281. if (of_find_property(np, "ti,vsel1-state-high", NULL))
  282. pdata->vsel1_def_state = 1;
  283. if (of_find_property(np, "ti,enable-pull-down", NULL))
  284. pdata->en_internal_pulldn = true;
  285. if (of_find_property(np, "ti,enable-vout-discharge", NULL))
  286. pdata->en_discharge = true;
  287. return pdata;
  288. }
  289. #if defined(CONFIG_OF)
  290. static const struct of_device_id tps62360_of_match[] = {
  291. { .compatible = "ti,tps62360", .data = (void *)TPS62360},
  292. { .compatible = "ti,tps62361", .data = (void *)TPS62361},
  293. { .compatible = "ti,tps62362", .data = (void *)TPS62362},
  294. { .compatible = "ti,tps62363", .data = (void *)TPS62363},
  295. {},
  296. };
  297. MODULE_DEVICE_TABLE(of, tps62360_of_match);
  298. #endif
  299. static int tps62360_probe(struct i2c_client *client,
  300. const struct i2c_device_id *id)
  301. {
  302. struct regulator_config config = { };
  303. struct tps62360_regulator_platform_data *pdata;
  304. struct regulator_dev *rdev;
  305. struct tps62360_chip *tps;
  306. int ret;
  307. int i;
  308. int chip_id;
  309. pdata = dev_get_platdata(&client->dev);
  310. tps = devm_kzalloc(&client->dev, sizeof(*tps), GFP_KERNEL);
  311. if (!tps)
  312. return -ENOMEM;
  313. tps->desc.name = client->name;
  314. tps->desc.id = 0;
  315. tps->desc.ops = &tps62360_dcdc_ops;
  316. tps->desc.type = REGULATOR_VOLTAGE;
  317. tps->desc.owner = THIS_MODULE;
  318. tps->desc.uV_step = 10000;
  319. if (client->dev.of_node) {
  320. const struct of_device_id *match;
  321. match = of_match_device(of_match_ptr(tps62360_of_match),
  322. &client->dev);
  323. if (!match) {
  324. dev_err(&client->dev, "Error: No device match found\n");
  325. return -ENODEV;
  326. }
  327. chip_id = (int)(long)match->data;
  328. if (!pdata)
  329. pdata = of_get_tps62360_platform_data(&client->dev,
  330. &tps->desc);
  331. } else if (id) {
  332. chip_id = id->driver_data;
  333. } else {
  334. dev_err(&client->dev, "No device tree match or id table match found\n");
  335. return -ENODEV;
  336. }
  337. if (!pdata) {
  338. dev_err(&client->dev, "%s(): Platform data not found\n",
  339. __func__);
  340. return -EIO;
  341. }
  342. tps->en_discharge = pdata->en_discharge;
  343. tps->en_internal_pulldn = pdata->en_internal_pulldn;
  344. tps->vsel0_gpio = pdata->vsel0_gpio;
  345. tps->vsel1_gpio = pdata->vsel1_gpio;
  346. tps->dev = &client->dev;
  347. switch (chip_id) {
  348. case TPS62360:
  349. case TPS62362:
  350. tps->desc.min_uV = TPS62360_BASE_VOLTAGE;
  351. tps->voltage_reg_mask = 0x3F;
  352. tps->desc.n_voltages = TPS62360_N_VOLTAGES;
  353. break;
  354. case TPS62361:
  355. case TPS62363:
  356. tps->desc.min_uV = TPS62361_BASE_VOLTAGE;
  357. tps->voltage_reg_mask = 0x7F;
  358. tps->desc.n_voltages = TPS62361_N_VOLTAGES;
  359. break;
  360. default:
  361. return -ENODEV;
  362. }
  363. tps->regmap = devm_regmap_init_i2c(client, &tps62360_regmap_config);
  364. if (IS_ERR(tps->regmap)) {
  365. ret = PTR_ERR(tps->regmap);
  366. dev_err(&client->dev,
  367. "%s(): regmap allocation failed with err %d\n",
  368. __func__, ret);
  369. return ret;
  370. }
  371. i2c_set_clientdata(client, tps);
  372. tps->curr_vset_id = (pdata->vsel1_def_state & 1) * 2 +
  373. (pdata->vsel0_def_state & 1);
  374. tps->lru_index[0] = tps->curr_vset_id;
  375. tps->valid_gpios = false;
  376. if (gpio_is_valid(tps->vsel0_gpio) && gpio_is_valid(tps->vsel1_gpio)) {
  377. int gpio_flags;
  378. gpio_flags = (pdata->vsel0_def_state) ?
  379. GPIOF_OUT_INIT_HIGH : GPIOF_OUT_INIT_LOW;
  380. ret = devm_gpio_request_one(&client->dev, tps->vsel0_gpio,
  381. gpio_flags, "tps62360-vsel0");
  382. if (ret) {
  383. dev_err(&client->dev,
  384. "%s(): Could not obtain vsel0 GPIO %d: %d\n",
  385. __func__, tps->vsel0_gpio, ret);
  386. return ret;
  387. }
  388. gpio_flags = (pdata->vsel1_def_state) ?
  389. GPIOF_OUT_INIT_HIGH : GPIOF_OUT_INIT_LOW;
  390. ret = devm_gpio_request_one(&client->dev, tps->vsel1_gpio,
  391. gpio_flags, "tps62360-vsel1");
  392. if (ret) {
  393. dev_err(&client->dev,
  394. "%s(): Could not obtain vsel1 GPIO %d: %d\n",
  395. __func__, tps->vsel1_gpio, ret);
  396. return ret;
  397. }
  398. tps->valid_gpios = true;
  399. /*
  400. * Initialize the lru index with vset_reg id
  401. * The index 0 will be most recently used and
  402. * set with the tps->curr_vset_id */
  403. for (i = 0; i < 4; ++i)
  404. tps->lru_index[i] = i;
  405. tps->lru_index[0] = tps->curr_vset_id;
  406. tps->lru_index[tps->curr_vset_id] = 0;
  407. }
  408. ret = tps62360_init_dcdc(tps, pdata);
  409. if (ret < 0) {
  410. dev_err(tps->dev, "%s(): Init failed with err = %d\n",
  411. __func__, ret);
  412. return ret;
  413. }
  414. config.dev = &client->dev;
  415. config.init_data = pdata->reg_init_data;
  416. config.driver_data = tps;
  417. config.of_node = client->dev.of_node;
  418. /* Register the regulators */
  419. rdev = devm_regulator_register(&client->dev, &tps->desc, &config);
  420. if (IS_ERR(rdev)) {
  421. dev_err(tps->dev,
  422. "%s(): regulator register failed with err %s\n",
  423. __func__, id->name);
  424. return PTR_ERR(rdev);
  425. }
  426. tps->rdev = rdev;
  427. return 0;
  428. }
  429. static void tps62360_shutdown(struct i2c_client *client)
  430. {
  431. struct tps62360_chip *tps = i2c_get_clientdata(client);
  432. int st;
  433. if (!tps->en_discharge)
  434. return;
  435. /* Configure the output discharge path */
  436. st = regmap_update_bits(tps->regmap, REG_RAMPCTRL, BIT(2), BIT(2));
  437. if (st < 0)
  438. dev_err(tps->dev,
  439. "%s(): register %d update failed with err %d\n",
  440. __func__, REG_RAMPCTRL, st);
  441. }
  442. static const struct i2c_device_id tps62360_id[] = {
  443. {.name = "tps62360", .driver_data = TPS62360},
  444. {.name = "tps62361", .driver_data = TPS62361},
  445. {.name = "tps62362", .driver_data = TPS62362},
  446. {.name = "tps62363", .driver_data = TPS62363},
  447. {},
  448. };
  449. MODULE_DEVICE_TABLE(i2c, tps62360_id);
  450. static struct i2c_driver tps62360_i2c_driver = {
  451. .driver = {
  452. .name = "tps62360",
  453. .of_match_table = of_match_ptr(tps62360_of_match),
  454. },
  455. .probe = tps62360_probe,
  456. .shutdown = tps62360_shutdown,
  457. .id_table = tps62360_id,
  458. };
  459. static int __init tps62360_init(void)
  460. {
  461. return i2c_add_driver(&tps62360_i2c_driver);
  462. }
  463. subsys_initcall(tps62360_init);
  464. static void __exit tps62360_cleanup(void)
  465. {
  466. i2c_del_driver(&tps62360_i2c_driver);
  467. }
  468. module_exit(tps62360_cleanup);
  469. MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
  470. MODULE_DESCRIPTION("TPS6236x voltage regulator driver");
  471. MODULE_LICENSE("GPL v2");