isl6271a-regulator.c 4.9 KB

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