tps62360-regulator.c 14 KB

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