isl6271a-regulator.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /*
  2. * isl6271a-regulator.c
  3. *
  4. * Support for Intersil ISL6271A voltage regulator
  5. *
  6. * Copyright (C) 2010 Marek Vasut <marek.vasut@gmail.com>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation version 2.
  11. *
  12. * This program is distributed "as is" WITHOUT ANY WARRANTY of any kind,
  13. * whether express or implied; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. */
  17. #include <linux/kernel.h>
  18. #include <linux/module.h>
  19. #include <linux/init.h>
  20. #include <linux/err.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/regulator/driver.h>
  23. #include <linux/i2c.h>
  24. #include <linux/delay.h>
  25. #include <linux/slab.h>
  26. #define ISL6271A_VOLTAGE_MIN 850000
  27. #define ISL6271A_VOLTAGE_MAX 1600000
  28. #define ISL6271A_VOLTAGE_STEP 50000
  29. /* PMIC details */
  30. struct isl_pmic {
  31. struct i2c_client *client;
  32. struct regulator_dev *rdev[3];
  33. struct mutex mtx;
  34. };
  35. static int isl6271a_get_voltage(struct regulator_dev *dev)
  36. {
  37. struct isl_pmic *pmic = rdev_get_drvdata(dev);
  38. int idx, data;
  39. mutex_lock(&pmic->mtx);
  40. idx = i2c_smbus_read_byte(pmic->client);
  41. if (idx < 0) {
  42. dev_err(&pmic->client->dev, "Error getting voltage\n");
  43. data = idx;
  44. goto out;
  45. }
  46. /* Convert the data from chip to microvolts */
  47. data = ISL6271A_VOLTAGE_MIN + (ISL6271A_VOLTAGE_STEP * (idx & 0xf));
  48. out:
  49. mutex_unlock(&pmic->mtx);
  50. return data;
  51. }
  52. static int isl6271a_set_voltage(struct regulator_dev *dev, int minuV, int maxuV)
  53. {
  54. struct isl_pmic *pmic = rdev_get_drvdata(dev);
  55. int vsel, err, data;
  56. if (minuV < ISL6271A_VOLTAGE_MIN || minuV > ISL6271A_VOLTAGE_MAX)
  57. return -EINVAL;
  58. if (maxuV < ISL6271A_VOLTAGE_MIN || maxuV > ISL6271A_VOLTAGE_MAX)
  59. return -EINVAL;
  60. /* Align to 50000 mV */
  61. vsel = minuV - (minuV % ISL6271A_VOLTAGE_STEP);
  62. /* If the result fell out of [minuV,maxuV] range, put it back */
  63. if (vsel < minuV)
  64. vsel += ISL6271A_VOLTAGE_STEP;
  65. /* Convert the microvolts to data for the chip */
  66. data = (vsel - ISL6271A_VOLTAGE_MIN) / ISL6271A_VOLTAGE_STEP;
  67. mutex_lock(&pmic->mtx);
  68. err = i2c_smbus_write_byte(pmic->client, data);
  69. if (err < 0)
  70. dev_err(&pmic->client->dev, "Error setting voltage\n");
  71. mutex_unlock(&pmic->mtx);
  72. return err;
  73. }
  74. static int isl6271a_list_voltage(struct regulator_dev *dev, unsigned selector)
  75. {
  76. return ISL6271A_VOLTAGE_MIN + (ISL6271A_VOLTAGE_STEP * selector);
  77. }
  78. static struct regulator_ops isl_core_ops = {
  79. .get_voltage = isl6271a_get_voltage,
  80. .set_voltage = isl6271a_set_voltage,
  81. .list_voltage = isl6271a_list_voltage,
  82. };
  83. static int isl6271a_get_fixed_voltage(struct regulator_dev *dev)
  84. {
  85. int id = rdev_get_id(dev);
  86. return (id == 1) ? 1100000 : 1300000;
  87. }
  88. static int isl6271a_list_fixed_voltage(struct regulator_dev *dev, unsigned selector)
  89. {
  90. int id = rdev_get_id(dev);
  91. return (id == 1) ? 1100000 : 1300000;
  92. }
  93. static struct regulator_ops isl_fixed_ops = {
  94. .get_voltage = isl6271a_get_fixed_voltage,
  95. .list_voltage = isl6271a_list_fixed_voltage,
  96. };
  97. static struct regulator_desc isl_rd[] = {
  98. {
  99. .name = "Core Buck",
  100. .id = 0,
  101. .n_voltages = 16,
  102. .ops = &isl_core_ops,
  103. .type = REGULATOR_VOLTAGE,
  104. .owner = THIS_MODULE,
  105. }, {
  106. .name = "LDO1",
  107. .id = 1,
  108. .n_voltages = 1,
  109. .ops = &isl_fixed_ops,
  110. .type = REGULATOR_VOLTAGE,
  111. .owner = THIS_MODULE,
  112. }, {
  113. .name = "LDO2",
  114. .id = 2,
  115. .n_voltages = 1,
  116. .ops = &isl_fixed_ops,
  117. .type = REGULATOR_VOLTAGE,
  118. .owner = THIS_MODULE,
  119. },
  120. };
  121. static int __devinit isl6271a_probe(struct i2c_client *i2c,
  122. const struct i2c_device_id *id)
  123. {
  124. struct regulator_init_data *init_data = i2c->dev.platform_data;
  125. struct isl_pmic *pmic;
  126. int err, i;
  127. if (!i2c_check_functionality(i2c->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  128. return -EIO;
  129. if (!init_data) {
  130. dev_err(&i2c->dev, "no platform data supplied\n");
  131. return -EIO;
  132. }
  133. pmic = kzalloc(sizeof(struct isl_pmic), GFP_KERNEL);
  134. if (!pmic)
  135. return -ENOMEM;
  136. pmic->client = i2c;
  137. mutex_init(&pmic->mtx);
  138. for (i = 0; i < 3; i++) {
  139. pmic->rdev[i] = regulator_register(&isl_rd[i], &i2c->dev,
  140. init_data, pmic);
  141. if (IS_ERR(pmic->rdev[i])) {
  142. dev_err(&i2c->dev, "failed to register %s\n", id->name);
  143. err = PTR_ERR(pmic->rdev);
  144. goto error;
  145. }
  146. }
  147. i2c_set_clientdata(i2c, pmic);
  148. return 0;
  149. error:
  150. while (--i >= 0)
  151. regulator_unregister(pmic->rdev[i]);
  152. kfree(pmic);
  153. return err;
  154. }
  155. static int __devexit isl6271a_remove(struct i2c_client *i2c)
  156. {
  157. struct isl_pmic *pmic = i2c_get_clientdata(i2c);
  158. int i;
  159. for (i = 0; i < 3; i++)
  160. regulator_unregister(pmic->rdev[i]);
  161. kfree(pmic);
  162. return 0;
  163. }
  164. static const struct i2c_device_id isl6271a_id[] = {
  165. {.name = "isl6271a", 0 },
  166. { },
  167. };
  168. MODULE_DEVICE_TABLE(i2c, isl6271a_id);
  169. static struct i2c_driver isl6271a_i2c_driver = {
  170. .driver = {
  171. .name = "isl6271a",
  172. .owner = THIS_MODULE,
  173. },
  174. .probe = isl6271a_probe,
  175. .remove = __devexit_p(isl6271a_remove),
  176. .id_table = isl6271a_id,
  177. };
  178. static int __init isl6271a_init(void)
  179. {
  180. return i2c_add_driver(&isl6271a_i2c_driver);
  181. }
  182. static void __exit isl6271a_cleanup(void)
  183. {
  184. i2c_del_driver(&isl6271a_i2c_driver);
  185. }
  186. subsys_initcall(isl6271a_init);
  187. module_exit(isl6271a_cleanup);
  188. MODULE_AUTHOR("Marek Vasut <marek.vasut@gmail.com>");
  189. MODULE_DESCRIPTION("Intersil ISL6271A voltage regulator driver");
  190. MODULE_LICENSE("GPL v2");