isl6271a-regulator.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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/slab.h>
  25. #define ISL6271A_VOLTAGE_MIN 850000
  26. #define ISL6271A_VOLTAGE_MAX 1600000
  27. #define ISL6271A_VOLTAGE_STEP 50000
  28. /* PMIC details */
  29. struct isl_pmic {
  30. struct i2c_client *client;
  31. struct regulator_dev *rdev[3];
  32. struct mutex mtx;
  33. };
  34. static int isl6271a_get_voltage(struct regulator_dev *dev)
  35. {
  36. struct isl_pmic *pmic = rdev_get_drvdata(dev);
  37. int idx, data;
  38. mutex_lock(&pmic->mtx);
  39. idx = i2c_smbus_read_byte(pmic->client);
  40. if (idx < 0) {
  41. dev_err(&pmic->client->dev, "Error getting voltage\n");
  42. data = idx;
  43. goto out;
  44. }
  45. /* Convert the data from chip to microvolts */
  46. data = ISL6271A_VOLTAGE_MIN + (ISL6271A_VOLTAGE_STEP * (idx & 0xf));
  47. out:
  48. mutex_unlock(&pmic->mtx);
  49. return data;
  50. }
  51. static int isl6271a_set_voltage(struct regulator_dev *dev,
  52. int minuV, int maxuV,
  53. unsigned *selector)
  54. {
  55. struct isl_pmic *pmic = rdev_get_drvdata(dev);
  56. int err, data;
  57. if (minuV < ISL6271A_VOLTAGE_MIN || minuV > ISL6271A_VOLTAGE_MAX)
  58. return -EINVAL;
  59. if (maxuV < ISL6271A_VOLTAGE_MIN || maxuV > ISL6271A_VOLTAGE_MAX)
  60. return -EINVAL;
  61. data = DIV_ROUND_UP(minuV - ISL6271A_VOLTAGE_MIN,
  62. ISL6271A_VOLTAGE_STEP);
  63. *selector = data;
  64. mutex_lock(&pmic->mtx);
  65. err = i2c_smbus_write_byte(pmic->client, data);
  66. if (err < 0)
  67. dev_err(&pmic->client->dev, "Error setting voltage\n");
  68. mutex_unlock(&pmic->mtx);
  69. return err;
  70. }
  71. static int isl6271a_list_voltage(struct regulator_dev *dev, unsigned selector)
  72. {
  73. return ISL6271A_VOLTAGE_MIN + (ISL6271A_VOLTAGE_STEP * selector);
  74. }
  75. static struct regulator_ops isl_core_ops = {
  76. .get_voltage = isl6271a_get_voltage,
  77. .set_voltage = isl6271a_set_voltage,
  78. .list_voltage = isl6271a_list_voltage,
  79. };
  80. static int isl6271a_get_fixed_voltage(struct regulator_dev *dev)
  81. {
  82. int id = rdev_get_id(dev);
  83. return (id == 1) ? 1100000 : 1300000;
  84. }
  85. static int isl6271a_list_fixed_voltage(struct regulator_dev *dev, unsigned selector)
  86. {
  87. int id = rdev_get_id(dev);
  88. return (id == 1) ? 1100000 : 1300000;
  89. }
  90. static struct regulator_ops isl_fixed_ops = {
  91. .get_voltage = isl6271a_get_fixed_voltage,
  92. .list_voltage = isl6271a_list_fixed_voltage,
  93. };
  94. static const struct regulator_desc isl_rd[] = {
  95. {
  96. .name = "Core Buck",
  97. .id = 0,
  98. .n_voltages = 16,
  99. .ops = &isl_core_ops,
  100. .type = REGULATOR_VOLTAGE,
  101. .owner = THIS_MODULE,
  102. }, {
  103. .name = "LDO1",
  104. .id = 1,
  105. .n_voltages = 1,
  106. .ops = &isl_fixed_ops,
  107. .type = REGULATOR_VOLTAGE,
  108. .owner = THIS_MODULE,
  109. }, {
  110. .name = "LDO2",
  111. .id = 2,
  112. .n_voltages = 1,
  113. .ops = &isl_fixed_ops,
  114. .type = REGULATOR_VOLTAGE,
  115. .owner = THIS_MODULE,
  116. },
  117. };
  118. static int __devinit isl6271a_probe(struct i2c_client *i2c,
  119. const struct i2c_device_id *id)
  120. {
  121. struct regulator_config config = { };
  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. pmic = devm_kzalloc(&i2c->dev, sizeof(struct isl_pmic), GFP_KERNEL);
  128. if (!pmic)
  129. return -ENOMEM;
  130. pmic->client = i2c;
  131. mutex_init(&pmic->mtx);
  132. for (i = 0; i < 3; i++) {
  133. config.dev = &i2c->dev;
  134. if (i == 0)
  135. config.init_data = init_data;
  136. else
  137. config.init_data = 0;
  138. config.driver_data = pmic;
  139. pmic->rdev[i] = regulator_register(&isl_rd[i], &config);
  140. if (IS_ERR(pmic->rdev[i])) {
  141. dev_err(&i2c->dev, "failed to register %s\n", id->name);
  142. err = PTR_ERR(pmic->rdev[i]);
  143. goto error;
  144. }
  145. }
  146. i2c_set_clientdata(i2c, pmic);
  147. return 0;
  148. error:
  149. while (--i >= 0)
  150. regulator_unregister(pmic->rdev[i]);
  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. return 0;
  160. }
  161. static const struct i2c_device_id isl6271a_id[] = {
  162. {.name = "isl6271a", 0 },
  163. { },
  164. };
  165. MODULE_DEVICE_TABLE(i2c, isl6271a_id);
  166. static struct i2c_driver isl6271a_i2c_driver = {
  167. .driver = {
  168. .name = "isl6271a",
  169. .owner = THIS_MODULE,
  170. },
  171. .probe = isl6271a_probe,
  172. .remove = __devexit_p(isl6271a_remove),
  173. .id_table = isl6271a_id,
  174. };
  175. static int __init isl6271a_init(void)
  176. {
  177. return i2c_add_driver(&isl6271a_i2c_driver);
  178. }
  179. static void __exit isl6271a_cleanup(void)
  180. {
  181. i2c_del_driver(&isl6271a_i2c_driver);
  182. }
  183. subsys_initcall(isl6271a_init);
  184. module_exit(isl6271a_cleanup);
  185. MODULE_AUTHOR("Marek Vasut <marek.vasut@gmail.com>");
  186. MODULE_DESCRIPTION("Intersil ISL6271A voltage regulator driver");
  187. MODULE_LICENSE("GPL v2");