max1586.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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/slab.h>
  26. #include <linux/regulator/max1586.h>
  27. #define MAX1586_V3_MAX_VSEL 31
  28. #define MAX1586_V6_MAX_VSEL 3
  29. #define MAX1586_V3_MIN_UV 700000
  30. #define MAX1586_V3_MAX_UV 1475000
  31. #define MAX1586_V6_MIN_UV 0
  32. #define MAX1586_V6_MAX_UV 3000000
  33. #define I2C_V3_SELECT (0 << 5)
  34. #define I2C_V6_SELECT (1 << 5)
  35. struct max1586_data {
  36. struct i2c_client *client;
  37. /* min/max V3 voltage */
  38. unsigned int min_uV;
  39. unsigned int max_uV;
  40. struct regulator_dev *rdev[0];
  41. };
  42. /*
  43. * V3 voltage
  44. * On I2C bus, sending a "x" byte to the max1586 means :
  45. * set V3 to 0.700V + (x & 0x1f) * 0.025V
  46. * This voltage can be increased by external resistors
  47. * R24 and R25=100kOhm as described in the data sheet.
  48. * The gain is approximately: 1 + R24/R25 + R24/185.5kOhm
  49. */
  50. static int max1586_v3_calc_voltage(struct max1586_data *max1586,
  51. unsigned selector)
  52. {
  53. unsigned range_uV = max1586->max_uV - max1586->min_uV;
  54. return max1586->min_uV + (selector * range_uV / MAX1586_V3_MAX_VSEL);
  55. }
  56. static int max1586_v3_set(struct regulator_dev *rdev, int min_uV, int max_uV,
  57. unsigned *selector)
  58. {
  59. struct max1586_data *max1586 = rdev_get_drvdata(rdev);
  60. struct i2c_client *client = max1586->client;
  61. unsigned range_uV = max1586->max_uV - max1586->min_uV;
  62. u8 v3_prog;
  63. if (min_uV > max1586->max_uV || max_uV < max1586->min_uV)
  64. return -EINVAL;
  65. if (min_uV < max1586->min_uV)
  66. min_uV = max1586->min_uV;
  67. *selector = ((min_uV - max1586->min_uV) * MAX1586_V3_MAX_VSEL +
  68. range_uV - 1) / range_uV;
  69. if (max1586_v3_calc_voltage(max1586, *selector) > max_uV)
  70. return -EINVAL;
  71. dev_dbg(&client->dev, "changing voltage v3 to %dmv\n",
  72. max1586_v3_calc_voltage(max1586, *selector) / 1000);
  73. v3_prog = I2C_V3_SELECT | (u8) *selector;
  74. return i2c_smbus_write_byte(client, v3_prog);
  75. }
  76. static int max1586_v3_list(struct regulator_dev *rdev, unsigned selector)
  77. {
  78. struct max1586_data *max1586 = rdev_get_drvdata(rdev);
  79. if (selector > MAX1586_V3_MAX_VSEL)
  80. return -EINVAL;
  81. return max1586_v3_calc_voltage(max1586, selector);
  82. }
  83. /*
  84. * V6 voltage
  85. * On I2C bus, sending a "x" byte to the max1586 means :
  86. * set V6 to either 0V, 1.8V, 2.5V, 3V depending on (x & 0x3)
  87. * As regulator framework doesn't accept voltages to be 0V, we use 1uV.
  88. */
  89. static int max1586_v6_calc_voltage(unsigned selector)
  90. {
  91. static int voltages_uv[] = { 1, 1800000, 2500000, 3000000 };
  92. return voltages_uv[selector];
  93. }
  94. static int max1586_v6_set(struct regulator_dev *rdev, int min_uV, int max_uV,
  95. unsigned int *selector)
  96. {
  97. struct i2c_client *client = rdev_get_drvdata(rdev);
  98. u8 v6_prog;
  99. if (min_uV < MAX1586_V6_MIN_UV || min_uV > MAX1586_V6_MAX_UV)
  100. return -EINVAL;
  101. if (max_uV < MAX1586_V6_MIN_UV || max_uV > MAX1586_V6_MAX_UV)
  102. return -EINVAL;
  103. if (min_uV < 1800000)
  104. *selector = 0;
  105. else if (min_uV < 2500000)
  106. *selector = 1;
  107. else if (min_uV < 3000000)
  108. *selector = 2;
  109. else if (min_uV >= 3000000)
  110. *selector = 3;
  111. if (max1586_v6_calc_voltage(*selector) > max_uV)
  112. return -EINVAL;
  113. dev_dbg(&client->dev, "changing voltage v6 to %dmv\n",
  114. max1586_v6_calc_voltage(*selector) / 1000);
  115. v6_prog = I2C_V6_SELECT | (u8) *selector;
  116. return i2c_smbus_write_byte(client, v6_prog);
  117. }
  118. static int max1586_v6_list(struct regulator_dev *rdev, unsigned selector)
  119. {
  120. if (selector > MAX1586_V6_MAX_VSEL)
  121. return -EINVAL;
  122. return max1586_v6_calc_voltage(selector);
  123. }
  124. /*
  125. * The Maxim 1586 controls V3 and V6 voltages, but offers no way of reading back
  126. * the set up value.
  127. */
  128. static struct regulator_ops max1586_v3_ops = {
  129. .set_voltage = max1586_v3_set,
  130. .list_voltage = max1586_v3_list,
  131. };
  132. static struct regulator_ops max1586_v6_ops = {
  133. .set_voltage = max1586_v6_set,
  134. .list_voltage = max1586_v6_list,
  135. };
  136. static struct regulator_desc max1586_reg[] = {
  137. {
  138. .name = "Output_V3",
  139. .id = MAX1586_V3,
  140. .ops = &max1586_v3_ops,
  141. .type = REGULATOR_VOLTAGE,
  142. .n_voltages = MAX1586_V3_MAX_VSEL + 1,
  143. .owner = THIS_MODULE,
  144. },
  145. {
  146. .name = "Output_V6",
  147. .id = MAX1586_V6,
  148. .ops = &max1586_v6_ops,
  149. .type = REGULATOR_VOLTAGE,
  150. .n_voltages = MAX1586_V6_MAX_VSEL + 1,
  151. .owner = THIS_MODULE,
  152. },
  153. };
  154. static int __devinit max1586_pmic_probe(struct i2c_client *client,
  155. const struct i2c_device_id *i2c_id)
  156. {
  157. struct regulator_dev **rdev;
  158. struct max1586_platform_data *pdata = client->dev.platform_data;
  159. struct max1586_data *max1586;
  160. int i, id, ret = -ENOMEM;
  161. max1586 = kzalloc(sizeof(struct max1586_data) +
  162. sizeof(struct regulator_dev *) * (MAX1586_V6 + 1),
  163. GFP_KERNEL);
  164. if (!max1586)
  165. goto out;
  166. max1586->client = client;
  167. if (!pdata->v3_gain) {
  168. ret = -EINVAL;
  169. goto out_unmap;
  170. }
  171. max1586->min_uV = MAX1586_V3_MIN_UV / 1000 * pdata->v3_gain / 1000;
  172. max1586->max_uV = MAX1586_V3_MAX_UV / 1000 * pdata->v3_gain / 1000;
  173. rdev = max1586->rdev;
  174. for (i = 0; i < pdata->num_subdevs && i <= MAX1586_V6; i++) {
  175. id = pdata->subdevs[i].id;
  176. if (!pdata->subdevs[i].platform_data)
  177. continue;
  178. if (id < MAX1586_V3 || id > MAX1586_V6) {
  179. dev_err(&client->dev, "invalid regulator id %d\n", id);
  180. goto err;
  181. }
  182. rdev[i] = regulator_register(&max1586_reg[id], &client->dev,
  183. pdata->subdevs[i].platform_data,
  184. max1586);
  185. if (IS_ERR(rdev[i])) {
  186. ret = PTR_ERR(rdev[i]);
  187. dev_err(&client->dev, "failed to register %s\n",
  188. max1586_reg[id].name);
  189. goto err;
  190. }
  191. }
  192. i2c_set_clientdata(client, max1586);
  193. dev_info(&client->dev, "Maxim 1586 regulator driver loaded\n");
  194. return 0;
  195. err:
  196. while (--i >= 0)
  197. regulator_unregister(rdev[i]);
  198. out_unmap:
  199. kfree(max1586);
  200. out:
  201. return ret;
  202. }
  203. static int __devexit max1586_pmic_remove(struct i2c_client *client)
  204. {
  205. struct max1586_data *max1586 = i2c_get_clientdata(client);
  206. int i;
  207. for (i = 0; i <= MAX1586_V6; i++)
  208. if (max1586->rdev[i])
  209. regulator_unregister(max1586->rdev[i]);
  210. kfree(max1586);
  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 = __devexit_p(max1586_pmic_remove),
  221. .driver = {
  222. .name = "max1586",
  223. .owner = THIS_MODULE,
  224. },
  225. .id_table = max1586_id,
  226. };
  227. static int __init max1586_pmic_init(void)
  228. {
  229. return i2c_add_driver(&max1586_pmic_driver);
  230. }
  231. subsys_initcall(max1586_pmic_init);
  232. static void __exit max1586_pmic_exit(void)
  233. {
  234. i2c_del_driver(&max1586_pmic_driver);
  235. }
  236. module_exit(max1586_pmic_exit);
  237. /* Module information */
  238. MODULE_DESCRIPTION("MAXIM 1586 voltage regulator driver");
  239. MODULE_AUTHOR("Robert Jarzmik");
  240. MODULE_LICENSE("GPL");