tps62360-regulator.c 14 KB

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