isl6271a-regulator.c 4.7 KB

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