isl6271a-regulator.c 4.5 KB

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