max1586.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /*
  2. * max1586.c -- Voltage and current regulation for the Maxim 1586
  3. *
  4. * Copyright (C) 2008 Robert Jarzmik
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. */
  20. #include <linux/module.h>
  21. #include <linux/err.h>
  22. #include <linux/i2c.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/regulator/driver.h>
  25. #include <linux/regulator/max1586.h>
  26. #define MAX1586_V3_MAX_VSEL 31
  27. #define MAX1586_V6_MAX_VSEL 3
  28. #define MAX1586_V3_MIN_UV 700000
  29. #define MAX1586_V3_MAX_UV 1475000
  30. #define MAX1586_V6_MIN_UV 0
  31. #define MAX1586_V6_MAX_UV 3000000
  32. #define I2C_V3_SELECT (0 << 5)
  33. #define I2C_V6_SELECT (1 << 5)
  34. struct max1586_data {
  35. struct i2c_client *client;
  36. /* min/max V3 voltage */
  37. unsigned int min_uV;
  38. unsigned int max_uV;
  39. struct regulator_dev *rdev[0];
  40. };
  41. /*
  42. * V3 voltage
  43. * On I2C bus, sending a "x" byte to the max1586 means :
  44. * set V3 to 0.700V + (x & 0x1f) * 0.025V
  45. * This voltage can be increased by external resistors
  46. * R24 and R25=100kOhm as described in the data sheet.
  47. * The gain is approximately: 1 + R24/R25 + R24/185.5kOhm
  48. */
  49. static int max1586_v3_calc_voltage(struct max1586_data *max1586,
  50. unsigned selector)
  51. {
  52. unsigned range_uV = max1586->max_uV - max1586->min_uV;
  53. return max1586->min_uV + (selector * range_uV / MAX1586_V3_MAX_VSEL);
  54. }
  55. static int max1586_v3_set(struct regulator_dev *rdev, int min_uV, int max_uV)
  56. {
  57. struct max1586_data *max1586 = rdev_get_drvdata(rdev);
  58. struct i2c_client *client = max1586->client;
  59. unsigned range_uV = max1586->max_uV - max1586->min_uV;
  60. unsigned selector;
  61. u8 v3_prog;
  62. if (min_uV > max1586->max_uV || max_uV < max1586->min_uV)
  63. return -EINVAL;
  64. if (min_uV < max1586->min_uV)
  65. min_uV = max1586->min_uV;
  66. selector = ((min_uV - max1586->min_uV) * MAX1586_V3_MAX_VSEL +
  67. range_uV - 1) / range_uV;
  68. if (max1586_v3_calc_voltage(max1586, selector) > max_uV)
  69. return -EINVAL;
  70. dev_dbg(&client->dev, "changing voltage v3 to %dmv\n",
  71. max1586_v3_calc_voltage(max1586, selector) / 1000);
  72. v3_prog = I2C_V3_SELECT | (u8) selector;
  73. return i2c_smbus_write_byte(client, v3_prog);
  74. }
  75. static int max1586_v3_list(struct regulator_dev *rdev, unsigned selector)
  76. {
  77. struct max1586_data *max1586 = rdev_get_drvdata(rdev);
  78. if (selector > MAX1586_V3_MAX_VSEL)
  79. return -EINVAL;
  80. return max1586_v3_calc_voltage(max1586, selector);
  81. }
  82. /*
  83. * V6 voltage
  84. * On I2C bus, sending a "x" byte to the max1586 means :
  85. * set V6 to either 0V, 1.8V, 2.5V, 3V depending on (x & 0x3)
  86. * As regulator framework doesn't accept voltages to be 0V, we use 1uV.
  87. */
  88. static int max1586_v6_calc_voltage(unsigned selector)
  89. {
  90. static int voltages_uv[] = { 1, 1800000, 2500000, 3000000 };
  91. return voltages_uv[selector];
  92. }
  93. static int max1586_v6_set(struct regulator_dev *rdev, int min_uV, int max_uV)
  94. {
  95. struct i2c_client *client = rdev_get_drvdata(rdev);
  96. unsigned selector;
  97. u8 v6_prog;
  98. if (min_uV < MAX1586_V6_MIN_UV || min_uV > MAX1586_V6_MAX_UV)
  99. return -EINVAL;
  100. if (max_uV < MAX1586_V6_MIN_UV || max_uV > MAX1586_V6_MAX_UV)
  101. return -EINVAL;
  102. if (min_uV >= 3000000)
  103. selector = 3;
  104. if (min_uV < 3000000)
  105. selector = 2;
  106. if (min_uV < 2500000)
  107. selector = 1;
  108. if (min_uV < 1800000)
  109. selector = 0;
  110. if (max1586_v6_calc_voltage(selector) > max_uV)
  111. return -EINVAL;
  112. dev_dbg(&client->dev, "changing voltage v6 to %dmv\n",
  113. max1586_v6_calc_voltage(selector) / 1000);
  114. v6_prog = I2C_V6_SELECT | (u8) selector;
  115. return i2c_smbus_write_byte(client, v6_prog);
  116. }
  117. static int max1586_v6_list(struct regulator_dev *rdev, unsigned selector)
  118. {
  119. if (selector > MAX1586_V6_MAX_VSEL)
  120. return -EINVAL;
  121. return max1586_v6_calc_voltage(selector);
  122. }
  123. /*
  124. * The Maxim 1586 controls V3 and V6 voltages, but offers no way of reading back
  125. * the set up value.
  126. */
  127. static struct regulator_ops max1586_v3_ops = {
  128. .set_voltage = max1586_v3_set,
  129. .list_voltage = max1586_v3_list,
  130. };
  131. static struct regulator_ops max1586_v6_ops = {
  132. .set_voltage = max1586_v6_set,
  133. .list_voltage = max1586_v6_list,
  134. };
  135. static struct regulator_desc max1586_reg[] = {
  136. {
  137. .name = "Output_V3",
  138. .id = MAX1586_V3,
  139. .ops = &max1586_v3_ops,
  140. .type = REGULATOR_VOLTAGE,
  141. .n_voltages = MAX1586_V3_MAX_VSEL + 1,
  142. .owner = THIS_MODULE,
  143. },
  144. {
  145. .name = "Output_V6",
  146. .id = MAX1586_V6,
  147. .ops = &max1586_v6_ops,
  148. .type = REGULATOR_VOLTAGE,
  149. .n_voltages = MAX1586_V6_MAX_VSEL + 1,
  150. .owner = THIS_MODULE,
  151. },
  152. };
  153. static int max1586_pmic_probe(struct i2c_client *client,
  154. const struct i2c_device_id *i2c_id)
  155. {
  156. struct regulator_dev **rdev;
  157. struct max1586_platform_data *pdata = client->dev.platform_data;
  158. struct max1586_data *max1586;
  159. int i, id, ret = -ENOMEM;
  160. max1586 = kzalloc(sizeof(struct max1586_data) +
  161. sizeof(struct regulator_dev *) * (MAX1586_V6 + 1),
  162. GFP_KERNEL);
  163. if (!max1586)
  164. goto out;
  165. max1586->client = client;
  166. if (!pdata->v3_gain) {
  167. ret = -EINVAL;
  168. goto out_unmap;
  169. }
  170. max1586->min_uV = MAX1586_V3_MIN_UV / 1000 * pdata->v3_gain / 1000;
  171. max1586->max_uV = MAX1586_V3_MAX_UV / 1000 * pdata->v3_gain / 1000;
  172. rdev = max1586->rdev;
  173. for (i = 0; i < pdata->num_subdevs && i <= MAX1586_V6; i++) {
  174. id = pdata->subdevs[i].id;
  175. if (!pdata->subdevs[i].platform_data)
  176. continue;
  177. if (id < MAX1586_V3 || id > MAX1586_V6) {
  178. dev_err(&client->dev, "invalid regulator id %d\n", id);
  179. goto err;
  180. }
  181. rdev[i] = regulator_register(&max1586_reg[id], &client->dev,
  182. pdata->subdevs[i].platform_data,
  183. max1586);
  184. if (IS_ERR(rdev[i])) {
  185. ret = PTR_ERR(rdev[i]);
  186. dev_err(&client->dev, "failed to register %s\n",
  187. max1586_reg[id].name);
  188. goto err;
  189. }
  190. }
  191. i2c_set_clientdata(client, rdev);
  192. dev_info(&client->dev, "Maxim 1586 regulator driver loaded\n");
  193. return 0;
  194. err:
  195. while (--i >= 0)
  196. regulator_unregister(rdev[i]);
  197. out_unmap:
  198. kfree(max1586);
  199. out:
  200. return ret;
  201. }
  202. static int max1586_pmic_remove(struct i2c_client *client)
  203. {
  204. struct regulator_dev **rdev = i2c_get_clientdata(client);
  205. int i;
  206. for (i = 0; i <= MAX1586_V6; i++)
  207. if (rdev[i])
  208. regulator_unregister(rdev[i]);
  209. kfree(rdev);
  210. i2c_set_clientdata(client, NULL);
  211. return 0;
  212. }
  213. static const struct i2c_device_id max1586_id[] = {
  214. { "max1586", 0 },
  215. { }
  216. };
  217. MODULE_DEVICE_TABLE(i2c, max1586_id);
  218. static struct i2c_driver max1586_pmic_driver = {
  219. .probe = max1586_pmic_probe,
  220. .remove = max1586_pmic_remove,
  221. .driver = {
  222. .name = "max1586",
  223. },
  224. .id_table = max1586_id,
  225. };
  226. static int __init max1586_pmic_init(void)
  227. {
  228. return i2c_add_driver(&max1586_pmic_driver);
  229. }
  230. subsys_initcall(max1586_pmic_init);
  231. static void __exit max1586_pmic_exit(void)
  232. {
  233. i2c_del_driver(&max1586_pmic_driver);
  234. }
  235. module_exit(max1586_pmic_exit);
  236. /* Module information */
  237. MODULE_DESCRIPTION("MAXIM 1586 voltage regulator driver");
  238. MODULE_AUTHOR("Robert Jarzmik");
  239. MODULE_LICENSE("GPL");