isl6271a-regulator.c 4.4 KB

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