da9052-hwmon.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /*
  2. * HWMON Driver for Dialog DA9052
  3. *
  4. * Copyright(c) 2012 Dialog Semiconductor Ltd.
  5. *
  6. * Author: David Dajun Chen <dchen@diasemi.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the
  10. * Free Software Foundation; either version 2 of the License, or (at your
  11. * option) any later version.
  12. *
  13. */
  14. #include <linux/err.h>
  15. #include <linux/hwmon.h>
  16. #include <linux/hwmon-sysfs.h>
  17. #include <linux/init.h>
  18. #include <linux/kernel.h>
  19. #include <linux/module.h>
  20. #include <linux/slab.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/mfd/da9052/da9052.h>
  23. #include <linux/mfd/da9052/reg.h>
  24. struct da9052_hwmon {
  25. struct da9052 *da9052;
  26. struct mutex hwmon_lock;
  27. };
  28. static const char * const input_names[] = {
  29. [DA9052_ADC_VDDOUT] = "VDDOUT",
  30. [DA9052_ADC_ICH] = "CHARGING CURRENT",
  31. [DA9052_ADC_TBAT] = "BATTERY TEMP",
  32. [DA9052_ADC_VBAT] = "BATTERY VOLTAGE",
  33. [DA9052_ADC_IN4] = "ADC IN4",
  34. [DA9052_ADC_IN5] = "ADC IN5",
  35. [DA9052_ADC_IN6] = "ADC IN6",
  36. [DA9052_ADC_TJUNC] = "BATTERY JUNCTION TEMP",
  37. [DA9052_ADC_VBBAT] = "BACK-UP BATTERY VOLTAGE",
  38. };
  39. /* Conversion function for VDDOUT and VBAT */
  40. static inline int volt_reg_to_mv(int value)
  41. {
  42. return DIV_ROUND_CLOSEST(value * 2000, 1023) + 2500;
  43. }
  44. /* Conversion function for ADC channels 4, 5 and 6 */
  45. static inline int input_reg_to_mv(int value)
  46. {
  47. return DIV_ROUND_CLOSEST(value * 2500, 1023);
  48. }
  49. /* Conversion function for VBBAT */
  50. static inline int vbbat_reg_to_mv(int value)
  51. {
  52. return DIV_ROUND_CLOSEST(value * 5000, 1023);
  53. }
  54. static inline int da9052_enable_vddout_channel(struct da9052 *da9052)
  55. {
  56. return da9052_reg_update(da9052, DA9052_ADC_CONT_REG,
  57. DA9052_ADCCONT_AUTOVDDEN,
  58. DA9052_ADCCONT_AUTOVDDEN);
  59. }
  60. static inline int da9052_disable_vddout_channel(struct da9052 *da9052)
  61. {
  62. return da9052_reg_update(da9052, DA9052_ADC_CONT_REG,
  63. DA9052_ADCCONT_AUTOVDDEN, 0);
  64. }
  65. static ssize_t da9052_read_vddout(struct device *dev,
  66. struct device_attribute *devattr, char *buf)
  67. {
  68. struct da9052_hwmon *hwmon = dev_get_drvdata(dev);
  69. int ret, vdd;
  70. mutex_lock(&hwmon->hwmon_lock);
  71. ret = da9052_enable_vddout_channel(hwmon->da9052);
  72. if (ret < 0)
  73. goto hwmon_err;
  74. vdd = da9052_reg_read(hwmon->da9052, DA9052_VDD_RES_REG);
  75. if (vdd < 0) {
  76. ret = vdd;
  77. goto hwmon_err_release;
  78. }
  79. ret = da9052_disable_vddout_channel(hwmon->da9052);
  80. if (ret < 0)
  81. goto hwmon_err;
  82. mutex_unlock(&hwmon->hwmon_lock);
  83. return sprintf(buf, "%d\n", volt_reg_to_mv(vdd));
  84. hwmon_err_release:
  85. da9052_disable_vddout_channel(hwmon->da9052);
  86. hwmon_err:
  87. mutex_unlock(&hwmon->hwmon_lock);
  88. return ret;
  89. }
  90. static ssize_t da9052_read_ich(struct device *dev,
  91. struct device_attribute *devattr, char *buf)
  92. {
  93. struct da9052_hwmon *hwmon = dev_get_drvdata(dev);
  94. int ret;
  95. ret = da9052_reg_read(hwmon->da9052, DA9052_ICHG_AV_REG);
  96. if (ret < 0)
  97. return ret;
  98. /* Equivalent to 3.9mA/bit in register ICHG_AV */
  99. return sprintf(buf, "%d\n", DIV_ROUND_CLOSEST(ret * 39, 10));
  100. }
  101. static ssize_t da9052_read_tbat(struct device *dev,
  102. struct device_attribute *devattr, char *buf)
  103. {
  104. struct da9052_hwmon *hwmon = dev_get_drvdata(dev);
  105. return sprintf(buf, "%d\n", da9052_adc_read_temp(hwmon->da9052));
  106. }
  107. static ssize_t da9052_read_vbat(struct device *dev,
  108. struct device_attribute *devattr, char *buf)
  109. {
  110. struct da9052_hwmon *hwmon = dev_get_drvdata(dev);
  111. int ret;
  112. ret = da9052_adc_manual_read(hwmon->da9052, DA9052_ADC_VBAT);
  113. if (ret < 0)
  114. return ret;
  115. return sprintf(buf, "%d\n", volt_reg_to_mv(ret));
  116. }
  117. static ssize_t da9052_read_misc_channel(struct device *dev,
  118. struct device_attribute *devattr,
  119. char *buf)
  120. {
  121. struct da9052_hwmon *hwmon = dev_get_drvdata(dev);
  122. int channel = to_sensor_dev_attr(devattr)->index;
  123. int ret;
  124. ret = da9052_adc_manual_read(hwmon->da9052, channel);
  125. if (ret < 0)
  126. return ret;
  127. return sprintf(buf, "%d\n", input_reg_to_mv(ret));
  128. }
  129. static ssize_t da9052_read_tjunc(struct device *dev,
  130. struct device_attribute *devattr, char *buf)
  131. {
  132. struct da9052_hwmon *hwmon = dev_get_drvdata(dev);
  133. int tjunc;
  134. int toffset;
  135. tjunc = da9052_reg_read(hwmon->da9052, DA9052_TJUNC_RES_REG);
  136. if (tjunc < 0)
  137. return tjunc;
  138. toffset = da9052_reg_read(hwmon->da9052, DA9052_T_OFFSET_REG);
  139. if (toffset < 0)
  140. return toffset;
  141. /*
  142. * Degrees celsius = 1.708 * (TJUNC_RES - T_OFFSET) - 108.8
  143. * T_OFFSET is a trim value used to improve accuracy of the result
  144. */
  145. return sprintf(buf, "%d\n", 1708 * (tjunc - toffset) - 108800);
  146. }
  147. static ssize_t da9052_read_vbbat(struct device *dev,
  148. struct device_attribute *devattr, char *buf)
  149. {
  150. struct da9052_hwmon *hwmon = dev_get_drvdata(dev);
  151. int ret;
  152. ret = da9052_adc_manual_read(hwmon->da9052, DA9052_ADC_VBBAT);
  153. if (ret < 0)
  154. return ret;
  155. return sprintf(buf, "%d\n", vbbat_reg_to_mv(ret));
  156. }
  157. static ssize_t show_label(struct device *dev,
  158. struct device_attribute *devattr, char *buf)
  159. {
  160. return sprintf(buf, "%s\n",
  161. input_names[to_sensor_dev_attr(devattr)->index]);
  162. }
  163. static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, da9052_read_vddout, NULL,
  164. DA9052_ADC_VDDOUT);
  165. static SENSOR_DEVICE_ATTR(in0_label, S_IRUGO, show_label, NULL,
  166. DA9052_ADC_VDDOUT);
  167. static SENSOR_DEVICE_ATTR(in3_input, S_IRUGO, da9052_read_vbat, NULL,
  168. DA9052_ADC_VBAT);
  169. static SENSOR_DEVICE_ATTR(in3_label, S_IRUGO, show_label, NULL,
  170. DA9052_ADC_VBAT);
  171. static SENSOR_DEVICE_ATTR(in4_input, S_IRUGO, da9052_read_misc_channel, NULL,
  172. DA9052_ADC_IN4);
  173. static SENSOR_DEVICE_ATTR(in4_label, S_IRUGO, show_label, NULL,
  174. DA9052_ADC_IN4);
  175. static SENSOR_DEVICE_ATTR(in5_input, S_IRUGO, da9052_read_misc_channel, NULL,
  176. DA9052_ADC_IN5);
  177. static SENSOR_DEVICE_ATTR(in5_label, S_IRUGO, show_label, NULL,
  178. DA9052_ADC_IN5);
  179. static SENSOR_DEVICE_ATTR(in6_input, S_IRUGO, da9052_read_misc_channel, NULL,
  180. DA9052_ADC_IN6);
  181. static SENSOR_DEVICE_ATTR(in6_label, S_IRUGO, show_label, NULL,
  182. DA9052_ADC_IN6);
  183. static SENSOR_DEVICE_ATTR(in9_input, S_IRUGO, da9052_read_vbbat, NULL,
  184. DA9052_ADC_VBBAT);
  185. static SENSOR_DEVICE_ATTR(in9_label, S_IRUGO, show_label, NULL,
  186. DA9052_ADC_VBBAT);
  187. static SENSOR_DEVICE_ATTR(curr1_input, S_IRUGO, da9052_read_ich, NULL,
  188. DA9052_ADC_ICH);
  189. static SENSOR_DEVICE_ATTR(curr1_label, S_IRUGO, show_label, NULL,
  190. DA9052_ADC_ICH);
  191. static SENSOR_DEVICE_ATTR(temp2_input, S_IRUGO, da9052_read_tbat, NULL,
  192. DA9052_ADC_TBAT);
  193. static SENSOR_DEVICE_ATTR(temp2_label, S_IRUGO, show_label, NULL,
  194. DA9052_ADC_TBAT);
  195. static SENSOR_DEVICE_ATTR(temp8_input, S_IRUGO, da9052_read_tjunc, NULL,
  196. DA9052_ADC_TJUNC);
  197. static SENSOR_DEVICE_ATTR(temp8_label, S_IRUGO, show_label, NULL,
  198. DA9052_ADC_TJUNC);
  199. static struct attribute *da9052_attrs[] = {
  200. &sensor_dev_attr_in0_input.dev_attr.attr,
  201. &sensor_dev_attr_in0_label.dev_attr.attr,
  202. &sensor_dev_attr_in3_input.dev_attr.attr,
  203. &sensor_dev_attr_in3_label.dev_attr.attr,
  204. &sensor_dev_attr_in4_input.dev_attr.attr,
  205. &sensor_dev_attr_in4_label.dev_attr.attr,
  206. &sensor_dev_attr_in5_input.dev_attr.attr,
  207. &sensor_dev_attr_in5_label.dev_attr.attr,
  208. &sensor_dev_attr_in6_input.dev_attr.attr,
  209. &sensor_dev_attr_in6_label.dev_attr.attr,
  210. &sensor_dev_attr_in9_input.dev_attr.attr,
  211. &sensor_dev_attr_in9_label.dev_attr.attr,
  212. &sensor_dev_attr_curr1_input.dev_attr.attr,
  213. &sensor_dev_attr_curr1_label.dev_attr.attr,
  214. &sensor_dev_attr_temp2_input.dev_attr.attr,
  215. &sensor_dev_attr_temp2_label.dev_attr.attr,
  216. &sensor_dev_attr_temp8_input.dev_attr.attr,
  217. &sensor_dev_attr_temp8_label.dev_attr.attr,
  218. NULL
  219. };
  220. ATTRIBUTE_GROUPS(da9052);
  221. static int da9052_hwmon_probe(struct platform_device *pdev)
  222. {
  223. struct device *dev = &pdev->dev;
  224. struct da9052_hwmon *hwmon;
  225. struct device *hwmon_dev;
  226. hwmon = devm_kzalloc(dev, sizeof(struct da9052_hwmon), GFP_KERNEL);
  227. if (!hwmon)
  228. return -ENOMEM;
  229. mutex_init(&hwmon->hwmon_lock);
  230. hwmon->da9052 = dev_get_drvdata(pdev->dev.parent);
  231. hwmon_dev = devm_hwmon_device_register_with_groups(dev, "da9052",
  232. hwmon,
  233. da9052_groups);
  234. return PTR_ERR_OR_ZERO(hwmon_dev);
  235. }
  236. static struct platform_driver da9052_hwmon_driver = {
  237. .probe = da9052_hwmon_probe,
  238. .driver = {
  239. .name = "da9052-hwmon",
  240. },
  241. };
  242. module_platform_driver(da9052_hwmon_driver);
  243. MODULE_AUTHOR("David Dajun Chen <dchen@diasemi.com>");
  244. MODULE_DESCRIPTION("DA9052 HWMON driver");
  245. MODULE_LICENSE("GPL");
  246. MODULE_ALIAS("platform:da9052-hwmon");