tps62360-regulator.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  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. enum chips {TPS62360, TPS62361, TPS62362, TPS62363};
  50. #define TPS62360_BASE_VOLTAGE 770000
  51. #define TPS62360_N_VOLTAGES 64
  52. #define TPS62361_BASE_VOLTAGE 500000
  53. #define TPS62361_N_VOLTAGES 128
  54. /* tps 62360 chip information */
  55. struct tps62360_chip {
  56. struct device *dev;
  57. struct regulator_desc desc;
  58. struct regulator_dev *rdev;
  59. struct regmap *regmap;
  60. int chip_id;
  61. int vsel0_gpio;
  62. int vsel1_gpio;
  63. int voltage_base;
  64. u8 voltage_reg_mask;
  65. bool en_internal_pulldn;
  66. bool en_force_pwm;
  67. bool en_discharge;
  68. bool valid_gpios;
  69. int lru_index[4];
  70. int curr_vset_vsel[4];
  71. int curr_vset_id;
  72. int change_uv_per_us;
  73. };
  74. /*
  75. * find_voltage_set_register: Find new voltage configuration register
  76. * (VSET) id.
  77. * The finding of the new VSET register will be based on the LRU mechanism.
  78. * Each VSET register will have different voltage configured . This
  79. * Function will look if any of the VSET register have requested voltage set
  80. * or not.
  81. * - If it is already there then it will make that register as most
  82. * recently used and return as found so that caller need not to set
  83. * the VSET register but need to set the proper gpios to select this
  84. * VSET register.
  85. * - If requested voltage is not found then it will use the least
  86. * recently mechanism to get new VSET register for new configuration
  87. * and will return not_found so that caller need to set new VSET
  88. * register and then gpios (both).
  89. */
  90. static bool find_voltage_set_register(struct tps62360_chip *tps,
  91. int req_vsel, int *vset_reg_id)
  92. {
  93. int i;
  94. bool found = false;
  95. int new_vset_reg = tps->lru_index[3];
  96. int found_index = 3;
  97. for (i = 0; i < 4; ++i) {
  98. if (tps->curr_vset_vsel[tps->lru_index[i]] == req_vsel) {
  99. new_vset_reg = tps->lru_index[i];
  100. found_index = i;
  101. found = true;
  102. goto update_lru_index;
  103. }
  104. }
  105. update_lru_index:
  106. for (i = found_index; i > 0; i--)
  107. tps->lru_index[i] = tps->lru_index[i - 1];
  108. tps->lru_index[0] = new_vset_reg;
  109. *vset_reg_id = new_vset_reg;
  110. return found;
  111. }
  112. static int tps62360_dcdc_get_voltage_sel(struct regulator_dev *dev)
  113. {
  114. struct tps62360_chip *tps = rdev_get_drvdata(dev);
  115. int vsel;
  116. unsigned int data;
  117. int ret;
  118. ret = regmap_read(tps->regmap, REG_VSET0 + tps->curr_vset_id, &data);
  119. if (ret < 0) {
  120. dev_err(tps->dev, "%s(): register %d read failed with err %d\n",
  121. __func__, REG_VSET0 + tps->curr_vset_id, ret);
  122. return ret;
  123. }
  124. vsel = (int)data & tps->voltage_reg_mask;
  125. return vsel;
  126. }
  127. static int tps62360_dcdc_set_voltage(struct regulator_dev *dev,
  128. int min_uV, int max_uV, unsigned *selector)
  129. {
  130. struct tps62360_chip *tps = rdev_get_drvdata(dev);
  131. int vsel;
  132. int ret;
  133. bool found = false;
  134. int new_vset_id = tps->curr_vset_id;
  135. if ((max_uV < min_uV) || (max_uV < tps->voltage_base))
  136. return -EINVAL;
  137. if (min_uV > (tps->voltage_base + (tps->desc.n_voltages - 1) * 10000))
  138. return -EINVAL;
  139. vsel = DIV_ROUND_UP(min_uV - tps->voltage_base, 10000);
  140. if (selector)
  141. *selector = (vsel & tps->voltage_reg_mask);
  142. /*
  143. * If gpios are available to select the VSET register then least
  144. * recently used register for new configuration.
  145. */
  146. if (tps->valid_gpios)
  147. found = find_voltage_set_register(tps, vsel, &new_vset_id);
  148. if (!found) {
  149. ret = regmap_update_bits(tps->regmap, REG_VSET0 + new_vset_id,
  150. tps->voltage_reg_mask, vsel);
  151. if (ret < 0) {
  152. dev_err(tps->dev,
  153. "%s(): register %d update failed with err %d\n",
  154. __func__, REG_VSET0 + new_vset_id, ret);
  155. return ret;
  156. }
  157. tps->curr_vset_id = new_vset_id;
  158. tps->curr_vset_vsel[new_vset_id] = vsel;
  159. }
  160. /* Select proper VSET register vio gpios */
  161. if (tps->valid_gpios) {
  162. gpio_set_value_cansleep(tps->vsel0_gpio, new_vset_id & 0x1);
  163. gpio_set_value_cansleep(tps->vsel1_gpio,
  164. (new_vset_id >> 1) & 0x1);
  165. }
  166. return 0;
  167. }
  168. static int tps62360_set_voltage_time_sel(struct regulator_dev *rdev,
  169. unsigned int old_selector, unsigned int new_selector)
  170. {
  171. struct tps62360_chip *tps = rdev_get_drvdata(rdev);
  172. int old_uV, new_uV;
  173. old_uV = regulator_list_voltage_linear(rdev, old_selector);
  174. if (old_uV < 0)
  175. return old_uV;
  176. new_uV = regulator_list_voltage_linear(rdev, new_selector);
  177. if (new_uV < 0)
  178. return new_uV;
  179. return DIV_ROUND_UP(abs(old_uV - new_uV), tps->change_uv_per_us);
  180. }
  181. static struct regulator_ops tps62360_dcdc_ops = {
  182. .get_voltage_sel = tps62360_dcdc_get_voltage_sel,
  183. .set_voltage = tps62360_dcdc_set_voltage,
  184. .list_voltage = regulator_list_voltage_linear,
  185. .set_voltage_time_sel = tps62360_set_voltage_time_sel,
  186. };
  187. static int __devinit tps62360_init_force_pwm(struct tps62360_chip *tps,
  188. struct tps62360_regulator_platform_data *pdata,
  189. int vset_id)
  190. {
  191. int ret;
  192. int bit = 0;
  193. if (pdata->en_force_pwm)
  194. bit = BIT(7);
  195. ret = regmap_update_bits(tps->regmap, REG_VSET0 + vset_id, BIT(7), bit);
  196. if (ret < 0)
  197. dev_err(tps->dev,
  198. "%s(): register %d update failed with err %d\n",
  199. __func__, REG_VSET0 + vset_id, ret);
  200. return ret;
  201. }
  202. static int __devinit tps62360_init_dcdc(struct tps62360_chip *tps,
  203. struct tps62360_regulator_platform_data *pdata)
  204. {
  205. int ret;
  206. int i;
  207. unsigned int ramp_ctrl;
  208. /* Initialize internal pull up/down control */
  209. if (tps->en_internal_pulldn)
  210. ret = regmap_write(tps->regmap, REG_CONTROL, 0xE0);
  211. else
  212. ret = regmap_write(tps->regmap, REG_CONTROL, 0x0);
  213. if (ret < 0) {
  214. dev_err(tps->dev,
  215. "%s(): register %d write failed with err %d\n",
  216. __func__, REG_CONTROL, ret);
  217. return ret;
  218. }
  219. /* Initialize force PWM mode */
  220. if (tps->valid_gpios) {
  221. for (i = 0; i < 4; ++i) {
  222. ret = tps62360_init_force_pwm(tps, pdata, i);
  223. if (ret < 0)
  224. return ret;
  225. }
  226. } else {
  227. ret = tps62360_init_force_pwm(tps, pdata, tps->curr_vset_id);
  228. if (ret < 0)
  229. return ret;
  230. }
  231. /* Reset output discharge path to reduce power consumption */
  232. ret = regmap_update_bits(tps->regmap, REG_RAMPCTRL, BIT(2), 0);
  233. if (ret < 0) {
  234. dev_err(tps->dev,
  235. "%s(): register %d update failed with err %d\n",
  236. __func__, REG_RAMPCTRL, ret);
  237. return ret;
  238. }
  239. /* Get ramp value from ramp control register */
  240. ret = regmap_read(tps->regmap, REG_RAMPCTRL, &ramp_ctrl);
  241. if (ret < 0) {
  242. dev_err(tps->dev,
  243. "%s(): register %d read failed with err %d\n",
  244. __func__, REG_RAMPCTRL, ret);
  245. return ret;
  246. }
  247. ramp_ctrl = (ramp_ctrl >> 4) & 0x7;
  248. /* ramp mV/us = 32/(2^ramp_ctrl) */
  249. tps->change_uv_per_us = DIV_ROUND_UP(32000, BIT(ramp_ctrl));
  250. return ret;
  251. }
  252. static const struct regmap_config tps62360_regmap_config = {
  253. .reg_bits = 8,
  254. .val_bits = 8,
  255. .max_register = REG_CHIPID,
  256. .cache_type = REGCACHE_RBTREE,
  257. };
  258. static struct tps62360_regulator_platform_data *
  259. of_get_tps62360_platform_data(struct device *dev)
  260. {
  261. struct tps62360_regulator_platform_data *pdata;
  262. struct device_node *np = dev->of_node;
  263. pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
  264. if (!pdata) {
  265. dev_err(dev, "Memory alloc failed for platform data\n");
  266. return NULL;
  267. }
  268. pdata->reg_init_data = of_get_regulator_init_data(dev, dev->of_node);
  269. if (!pdata->reg_init_data) {
  270. dev_err(dev, "Not able to get OF regulator init data\n");
  271. return NULL;
  272. }
  273. pdata->vsel0_gpio = of_get_named_gpio(np, "vsel0-gpio", 0);
  274. pdata->vsel1_gpio = of_get_named_gpio(np, "vsel1-gpio", 0);
  275. if (of_find_property(np, "ti,vsel0-state-high", NULL))
  276. pdata->vsel0_def_state = 1;
  277. if (of_find_property(np, "ti,vsel1-state-high", NULL))
  278. pdata->vsel1_def_state = 1;
  279. if (of_find_property(np, "ti,enable-pull-down", NULL))
  280. pdata->en_internal_pulldn = true;
  281. if (of_find_property(np, "ti,enable-force-pwm", NULL))
  282. pdata->en_force_pwm = true;
  283. if (of_find_property(np, "ti,enable-vout-discharge", NULL))
  284. pdata->en_discharge = true;
  285. return pdata;
  286. }
  287. #if defined(CONFIG_OF)
  288. static const struct of_device_id tps62360_of_match[] = {
  289. { .compatible = "ti,tps62360", .data = (void *)TPS62360},
  290. { .compatible = "ti,tps62361", .data = (void *)TPS62361},
  291. { .compatible = "ti,tps62362", .data = (void *)TPS62362},
  292. { .compatible = "ti,tps62363", .data = (void *)TPS62363},
  293. {},
  294. }
  295. MODULE_DEVICE_TABLE(of, tps62360_of_match);
  296. #endif
  297. static int __devinit tps62360_probe(struct i2c_client *client,
  298. const struct i2c_device_id *id)
  299. {
  300. struct regulator_config config = { };
  301. struct tps62360_regulator_platform_data *pdata;
  302. struct regulator_dev *rdev;
  303. struct tps62360_chip *tps;
  304. int ret;
  305. int i;
  306. int chip_id;
  307. pdata = client->dev.platform_data;
  308. chip_id = id->driver_data;
  309. if (client->dev.of_node) {
  310. const struct of_device_id *match;
  311. match = of_match_device(of_match_ptr(tps62360_of_match),
  312. &client->dev);
  313. if (!match) {
  314. dev_err(&client->dev, "Error: No device match found\n");
  315. return -ENODEV;
  316. }
  317. chip_id = (int)match->data;
  318. if (!pdata)
  319. pdata = of_get_tps62360_platform_data(&client->dev);
  320. }
  321. if (!pdata) {
  322. dev_err(&client->dev, "%s(): Platform data not found\n",
  323. __func__);
  324. return -EIO;
  325. }
  326. tps = devm_kzalloc(&client->dev, sizeof(*tps), GFP_KERNEL);
  327. if (!tps) {
  328. dev_err(&client->dev, "%s(): Memory allocation failed\n",
  329. __func__);
  330. return -ENOMEM;
  331. }
  332. tps->en_force_pwm = pdata->en_force_pwm;
  333. tps->en_discharge = pdata->en_discharge;
  334. tps->en_internal_pulldn = pdata->en_internal_pulldn;
  335. tps->vsel0_gpio = pdata->vsel0_gpio;
  336. tps->vsel1_gpio = pdata->vsel1_gpio;
  337. tps->dev = &client->dev;
  338. switch (chip_id) {
  339. case TPS62360:
  340. case TPS62362:
  341. tps->voltage_base = TPS62360_BASE_VOLTAGE;
  342. tps->voltage_reg_mask = 0x3F;
  343. tps->desc.n_voltages = TPS62360_N_VOLTAGES;
  344. break;
  345. case TPS62361:
  346. case TPS62363:
  347. tps->voltage_base = TPS62361_BASE_VOLTAGE;
  348. tps->voltage_reg_mask = 0x7F;
  349. tps->desc.n_voltages = TPS62361_N_VOLTAGES;
  350. break;
  351. default:
  352. return -ENODEV;
  353. }
  354. tps->desc.name = id->name;
  355. tps->desc.id = 0;
  356. tps->desc.ops = &tps62360_dcdc_ops;
  357. tps->desc.type = REGULATOR_VOLTAGE;
  358. tps->desc.owner = THIS_MODULE;
  359. tps->desc.min_uV = tps->voltage_base;
  360. tps->desc.uV_step = 10000;
  361. tps->regmap = devm_regmap_init_i2c(client, &tps62360_regmap_config);
  362. if (IS_ERR(tps->regmap)) {
  363. ret = PTR_ERR(tps->regmap);
  364. dev_err(&client->dev,
  365. "%s(): regmap allocation failed with err %d\n",
  366. __func__, ret);
  367. return ret;
  368. }
  369. i2c_set_clientdata(client, tps);
  370. tps->curr_vset_id = (pdata->vsel1_def_state & 1) * 2 +
  371. (pdata->vsel0_def_state & 1);
  372. tps->lru_index[0] = tps->curr_vset_id;
  373. tps->valid_gpios = false;
  374. if (gpio_is_valid(tps->vsel0_gpio) && gpio_is_valid(tps->vsel1_gpio)) {
  375. int gpio_flags;
  376. gpio_flags = (pdata->vsel0_def_state) ?
  377. GPIOF_OUT_INIT_HIGH : GPIOF_OUT_INIT_LOW;
  378. ret = gpio_request_one(tps->vsel0_gpio,
  379. gpio_flags, "tps62360-vsel0");
  380. if (ret) {
  381. dev_err(&client->dev,
  382. "%s(): Could not obtain vsel0 GPIO %d: %d\n",
  383. __func__, tps->vsel0_gpio, ret);
  384. goto err_gpio0;
  385. }
  386. gpio_flags = (pdata->vsel1_def_state) ?
  387. GPIOF_OUT_INIT_HIGH : GPIOF_OUT_INIT_LOW;
  388. ret = gpio_request_one(tps->vsel1_gpio,
  389. gpio_flags, "tps62360-vsel1");
  390. if (ret) {
  391. dev_err(&client->dev,
  392. "%s(): Could not obtain vsel1 GPIO %d: %d\n",
  393. __func__, tps->vsel1_gpio, ret);
  394. goto err_gpio1;
  395. }
  396. tps->valid_gpios = true;
  397. /*
  398. * Initialize the lru index with vset_reg id
  399. * The index 0 will be most recently used and
  400. * set with the tps->curr_vset_id */
  401. for (i = 0; i < 4; ++i)
  402. tps->lru_index[i] = i;
  403. tps->lru_index[0] = tps->curr_vset_id;
  404. tps->lru_index[tps->curr_vset_id] = 0;
  405. }
  406. ret = tps62360_init_dcdc(tps, pdata);
  407. if (ret < 0) {
  408. dev_err(tps->dev, "%s(): Init failed with err = %d\n",
  409. __func__, ret);
  410. goto err_init;
  411. }
  412. config.dev = &client->dev;
  413. config.init_data = pdata->reg_init_data;
  414. config.driver_data = tps;
  415. /* Register the regulators */
  416. rdev = regulator_register(&tps->desc, &config);
  417. if (IS_ERR(rdev)) {
  418. dev_err(tps->dev,
  419. "%s(): regulator register failed with err %s\n",
  420. __func__, id->name);
  421. ret = PTR_ERR(rdev);
  422. goto err_init;
  423. }
  424. tps->rdev = rdev;
  425. return 0;
  426. err_init:
  427. if (gpio_is_valid(tps->vsel1_gpio))
  428. gpio_free(tps->vsel1_gpio);
  429. err_gpio1:
  430. if (gpio_is_valid(tps->vsel0_gpio))
  431. gpio_free(tps->vsel0_gpio);
  432. err_gpio0:
  433. return ret;
  434. }
  435. /**
  436. * tps62360_remove - tps62360 driver i2c remove handler
  437. * @client: i2c driver client device structure
  438. *
  439. * Unregister TPS driver as an i2c client device driver
  440. */
  441. static int __devexit tps62360_remove(struct i2c_client *client)
  442. {
  443. struct tps62360_chip *tps = i2c_get_clientdata(client);
  444. if (gpio_is_valid(tps->vsel1_gpio))
  445. gpio_free(tps->vsel1_gpio);
  446. if (gpio_is_valid(tps->vsel0_gpio))
  447. gpio_free(tps->vsel0_gpio);
  448. regulator_unregister(tps->rdev);
  449. return 0;
  450. }
  451. static void tps62360_shutdown(struct i2c_client *client)
  452. {
  453. struct tps62360_chip *tps = i2c_get_clientdata(client);
  454. int st;
  455. if (!tps->en_discharge)
  456. return;
  457. /* Configure the output discharge path */
  458. st = regmap_update_bits(tps->regmap, REG_RAMPCTRL, BIT(2), BIT(2));
  459. if (st < 0)
  460. dev_err(tps->dev,
  461. "%s(): register %d update failed with err %d\n",
  462. __func__, REG_RAMPCTRL, st);
  463. }
  464. static const struct i2c_device_id tps62360_id[] = {
  465. {.name = "tps62360", .driver_data = TPS62360},
  466. {.name = "tps62361", .driver_data = TPS62361},
  467. {.name = "tps62362", .driver_data = TPS62362},
  468. {.name = "tps62363", .driver_data = TPS62363},
  469. {},
  470. };
  471. MODULE_DEVICE_TABLE(i2c, tps62360_id);
  472. static struct i2c_driver tps62360_i2c_driver = {
  473. .driver = {
  474. .name = "tps62360",
  475. .owner = THIS_MODULE,
  476. .of_match_table = of_match_ptr(tps62360_of_match),
  477. },
  478. .probe = tps62360_probe,
  479. .remove = __devexit_p(tps62360_remove),
  480. .shutdown = tps62360_shutdown,
  481. .id_table = tps62360_id,
  482. };
  483. static int __init tps62360_init(void)
  484. {
  485. return i2c_add_driver(&tps62360_i2c_driver);
  486. }
  487. subsys_initcall(tps62360_init);
  488. static void __exit tps62360_cleanup(void)
  489. {
  490. i2c_del_driver(&tps62360_i2c_driver);
  491. }
  492. module_exit(tps62360_cleanup);
  493. MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
  494. MODULE_DESCRIPTION("TPS6236x voltage regulator driver");
  495. MODULE_LICENSE("GPL v2");