isl6271a-regulator.c 5.1 KB

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