isl6271a-regulator.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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,
  53. int minuV, int maxuV,
  54. unsigned *selector)
  55. {
  56. struct isl_pmic *pmic = rdev_get_drvdata(dev);
  57. int vsel, err, data;
  58. if (minuV < ISL6271A_VOLTAGE_MIN || minuV > ISL6271A_VOLTAGE_MAX)
  59. return -EINVAL;
  60. if (maxuV < ISL6271A_VOLTAGE_MIN || maxuV > ISL6271A_VOLTAGE_MAX)
  61. return -EINVAL;
  62. /* Align to 50000 mV */
  63. vsel = minuV - (minuV % ISL6271A_VOLTAGE_STEP);
  64. /* If the result fell out of [minuV,maxuV] range, put it back */
  65. if (vsel < minuV)
  66. vsel += ISL6271A_VOLTAGE_STEP;
  67. /* Convert the microvolts to data for the chip */
  68. data = (vsel - ISL6271A_VOLTAGE_MIN) / ISL6271A_VOLTAGE_STEP;
  69. *selector = data;
  70. mutex_lock(&pmic->mtx);
  71. err = i2c_smbus_write_byte(pmic->client, data);
  72. if (err < 0)
  73. dev_err(&pmic->client->dev, "Error setting voltage\n");
  74. mutex_unlock(&pmic->mtx);
  75. return err;
  76. }
  77. static int isl6271a_list_voltage(struct regulator_dev *dev, unsigned selector)
  78. {
  79. return ISL6271A_VOLTAGE_MIN + (ISL6271A_VOLTAGE_STEP * selector);
  80. }
  81. static struct regulator_ops isl_core_ops = {
  82. .get_voltage = isl6271a_get_voltage,
  83. .set_voltage = isl6271a_set_voltage,
  84. .list_voltage = isl6271a_list_voltage,
  85. };
  86. static int isl6271a_get_fixed_voltage(struct regulator_dev *dev)
  87. {
  88. int id = rdev_get_id(dev);
  89. return (id == 1) ? 1100000 : 1300000;
  90. }
  91. static int isl6271a_list_fixed_voltage(struct regulator_dev *dev, unsigned selector)
  92. {
  93. int id = rdev_get_id(dev);
  94. return (id == 1) ? 1100000 : 1300000;
  95. }
  96. static struct regulator_ops isl_fixed_ops = {
  97. .get_voltage = isl6271a_get_fixed_voltage,
  98. .list_voltage = isl6271a_list_fixed_voltage,
  99. };
  100. static struct regulator_desc isl_rd[] = {
  101. {
  102. .name = "Core Buck",
  103. .id = 0,
  104. .n_voltages = 16,
  105. .ops = &isl_core_ops,
  106. .type = REGULATOR_VOLTAGE,
  107. .owner = THIS_MODULE,
  108. }, {
  109. .name = "LDO1",
  110. .id = 1,
  111. .n_voltages = 1,
  112. .ops = &isl_fixed_ops,
  113. .type = REGULATOR_VOLTAGE,
  114. .owner = THIS_MODULE,
  115. }, {
  116. .name = "LDO2",
  117. .id = 2,
  118. .n_voltages = 1,
  119. .ops = &isl_fixed_ops,
  120. .type = REGULATOR_VOLTAGE,
  121. .owner = THIS_MODULE,
  122. },
  123. };
  124. static int __devinit isl6271a_probe(struct i2c_client *i2c,
  125. const struct i2c_device_id *id)
  126. {
  127. struct regulator_init_data *init_data = i2c->dev.platform_data;
  128. struct isl_pmic *pmic;
  129. int err, i;
  130. if (!i2c_check_functionality(i2c->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  131. return -EIO;
  132. if (!init_data) {
  133. dev_err(&i2c->dev, "no platform data supplied\n");
  134. return -EIO;
  135. }
  136. pmic = kzalloc(sizeof(struct isl_pmic), GFP_KERNEL);
  137. if (!pmic)
  138. return -ENOMEM;
  139. pmic->client = i2c;
  140. mutex_init(&pmic->mtx);
  141. for (i = 0; i < 3; i++) {
  142. pmic->rdev[i] = regulator_register(&isl_rd[i], &i2c->dev,
  143. init_data, pmic);
  144. if (IS_ERR(pmic->rdev[i])) {
  145. dev_err(&i2c->dev, "failed to register %s\n", id->name);
  146. err = PTR_ERR(pmic->rdev[i]);
  147. goto error;
  148. }
  149. }
  150. i2c_set_clientdata(i2c, pmic);
  151. return 0;
  152. error:
  153. while (--i >= 0)
  154. regulator_unregister(pmic->rdev[i]);
  155. kfree(pmic);
  156. return err;
  157. }
  158. static int __devexit isl6271a_remove(struct i2c_client *i2c)
  159. {
  160. struct isl_pmic *pmic = i2c_get_clientdata(i2c);
  161. int i;
  162. for (i = 0; i < 3; i++)
  163. regulator_unregister(pmic->rdev[i]);
  164. kfree(pmic);
  165. return 0;
  166. }
  167. static const struct i2c_device_id isl6271a_id[] = {
  168. {.name = "isl6271a", 0 },
  169. { },
  170. };
  171. MODULE_DEVICE_TABLE(i2c, isl6271a_id);
  172. static struct i2c_driver isl6271a_i2c_driver = {
  173. .driver = {
  174. .name = "isl6271a",
  175. .owner = THIS_MODULE,
  176. },
  177. .probe = isl6271a_probe,
  178. .remove = __devexit_p(isl6271a_remove),
  179. .id_table = isl6271a_id,
  180. };
  181. static int __init isl6271a_init(void)
  182. {
  183. return i2c_add_driver(&isl6271a_i2c_driver);
  184. }
  185. static void __exit isl6271a_cleanup(void)
  186. {
  187. i2c_del_driver(&isl6271a_i2c_driver);
  188. }
  189. subsys_initcall(isl6271a_init);
  190. module_exit(isl6271a_cleanup);
  191. MODULE_AUTHOR("Marek Vasut <marek.vasut@gmail.com>");
  192. MODULE_DESCRIPTION("Intersil ISL6271A voltage regulator driver");
  193. MODULE_LICENSE("GPL v2");