da9055-hwmon.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /*
  2. * HWMON Driver for Dialog DA9055
  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/delay.h>
  15. #include <linux/err.h>
  16. #include <linux/hwmon.h>
  17. #include <linux/hwmon-sysfs.h>
  18. #include <linux/init.h>
  19. #include <linux/kernel.h>
  20. #include <linux/module.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/completion.h>
  23. #include <linux/mfd/da9055/core.h>
  24. #include <linux/mfd/da9055/reg.h>
  25. #define DA9055_ADCIN_DIV 102
  26. #define DA9055_VSYS_DIV 85
  27. #define DA9055_ADC_VSYS 0
  28. #define DA9055_ADC_ADCIN1 1
  29. #define DA9055_ADC_ADCIN2 2
  30. #define DA9055_ADC_ADCIN3 3
  31. #define DA9055_ADC_TJUNC 4
  32. struct da9055_hwmon {
  33. struct da9055 *da9055;
  34. struct mutex hwmon_lock;
  35. struct mutex irq_lock;
  36. struct completion done;
  37. };
  38. static const char * const input_names[] = {
  39. [DA9055_ADC_VSYS] = "VSYS",
  40. [DA9055_ADC_ADCIN1] = "ADC IN1",
  41. [DA9055_ADC_ADCIN2] = "ADC IN2",
  42. [DA9055_ADC_ADCIN3] = "ADC IN3",
  43. [DA9055_ADC_TJUNC] = "CHIP TEMP",
  44. };
  45. static const u8 chan_mux[DA9055_ADC_TJUNC + 1] = {
  46. [DA9055_ADC_VSYS] = DA9055_ADC_MUX_VSYS,
  47. [DA9055_ADC_ADCIN1] = DA9055_ADC_MUX_ADCIN1,
  48. [DA9055_ADC_ADCIN2] = DA9055_ADC_MUX_ADCIN2,
  49. [DA9055_ADC_ADCIN3] = DA9055_ADC_MUX_ADCIN3,
  50. [DA9055_ADC_TJUNC] = DA9055_ADC_MUX_T_SENSE,
  51. };
  52. static int da9055_adc_manual_read(struct da9055_hwmon *hwmon,
  53. unsigned char channel)
  54. {
  55. int ret;
  56. unsigned short calc_data;
  57. unsigned short data;
  58. unsigned char mux_sel;
  59. struct da9055 *da9055 = hwmon->da9055;
  60. if (channel > DA9055_ADC_TJUNC)
  61. return -EINVAL;
  62. mutex_lock(&hwmon->irq_lock);
  63. /* Selects desired MUX for manual conversion */
  64. mux_sel = chan_mux[channel] | DA9055_ADC_MAN_CONV;
  65. ret = da9055_reg_write(da9055, DA9055_REG_ADC_MAN, mux_sel);
  66. if (ret < 0)
  67. goto err;
  68. /* Wait for an interrupt */
  69. if (!wait_for_completion_timeout(&hwmon->done,
  70. msecs_to_jiffies(500))) {
  71. dev_err(da9055->dev,
  72. "timeout waiting for ADC conversion interrupt\n");
  73. ret = -ETIMEDOUT;
  74. goto err;
  75. }
  76. ret = da9055_reg_read(da9055, DA9055_REG_ADC_RES_H);
  77. if (ret < 0)
  78. goto err;
  79. calc_data = (unsigned short)ret;
  80. data = calc_data << 2;
  81. ret = da9055_reg_read(da9055, DA9055_REG_ADC_RES_L);
  82. if (ret < 0)
  83. goto err;
  84. calc_data = (unsigned short)(ret & DA9055_ADC_LSB_MASK);
  85. data |= calc_data;
  86. ret = data;
  87. err:
  88. mutex_unlock(&hwmon->irq_lock);
  89. return ret;
  90. }
  91. static irqreturn_t da9055_auxadc_irq(int irq, void *irq_data)
  92. {
  93. struct da9055_hwmon *hwmon = irq_data;
  94. complete(&hwmon->done);
  95. return IRQ_HANDLED;
  96. }
  97. /* Conversion function for VSYS and ADCINx */
  98. static inline int volt_reg_to_mv(int value, int channel)
  99. {
  100. if (channel == DA9055_ADC_VSYS)
  101. return DIV_ROUND_CLOSEST(value * 1000, DA9055_VSYS_DIV) + 2500;
  102. else
  103. return DIV_ROUND_CLOSEST(value * 1000, DA9055_ADCIN_DIV);
  104. }
  105. static int da9055_enable_auto_mode(struct da9055 *da9055, int channel)
  106. {
  107. return da9055_reg_update(da9055, DA9055_REG_ADC_CONT, 1 << channel,
  108. 1 << channel);
  109. }
  110. static int da9055_disable_auto_mode(struct da9055 *da9055, int channel)
  111. {
  112. return da9055_reg_update(da9055, DA9055_REG_ADC_CONT, 1 << channel, 0);
  113. }
  114. static ssize_t da9055_read_auto_ch(struct device *dev,
  115. struct device_attribute *devattr, char *buf)
  116. {
  117. struct da9055_hwmon *hwmon = dev_get_drvdata(dev);
  118. int ret, adc;
  119. int channel = to_sensor_dev_attr(devattr)->index;
  120. mutex_lock(&hwmon->hwmon_lock);
  121. ret = da9055_enable_auto_mode(hwmon->da9055, channel);
  122. if (ret < 0)
  123. goto hwmon_err;
  124. usleep_range(10000, 10500);
  125. adc = da9055_reg_read(hwmon->da9055, DA9055_REG_VSYS_RES + channel);
  126. if (adc < 0) {
  127. ret = adc;
  128. goto hwmon_err_release;
  129. }
  130. ret = da9055_disable_auto_mode(hwmon->da9055, channel);
  131. if (ret < 0)
  132. goto hwmon_err;
  133. mutex_unlock(&hwmon->hwmon_lock);
  134. return sprintf(buf, "%d\n", volt_reg_to_mv(adc, channel));
  135. hwmon_err_release:
  136. da9055_disable_auto_mode(hwmon->da9055, channel);
  137. hwmon_err:
  138. mutex_unlock(&hwmon->hwmon_lock);
  139. return ret;
  140. }
  141. static ssize_t da9055_read_tjunc(struct device *dev,
  142. struct device_attribute *devattr, char *buf)
  143. {
  144. struct da9055_hwmon *hwmon = dev_get_drvdata(dev);
  145. int tjunc;
  146. int toffset;
  147. tjunc = da9055_adc_manual_read(hwmon, DA9055_ADC_TJUNC);
  148. if (tjunc < 0)
  149. return tjunc;
  150. toffset = da9055_reg_read(hwmon->da9055, DA9055_REG_T_OFFSET);
  151. if (toffset < 0)
  152. return toffset;
  153. /*
  154. * Degrees celsius = -0.4084 * (ADC_RES - T_OFFSET) + 307.6332
  155. * T_OFFSET is a trim value used to improve accuracy of the result
  156. */
  157. return sprintf(buf, "%d\n", DIV_ROUND_CLOSEST(-4084 * (tjunc - toffset)
  158. + 3076332, 10000));
  159. }
  160. static ssize_t show_label(struct device *dev,
  161. struct device_attribute *devattr, char *buf)
  162. {
  163. return sprintf(buf, "%s\n",
  164. input_names[to_sensor_dev_attr(devattr)->index]);
  165. }
  166. static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, da9055_read_auto_ch, NULL,
  167. DA9055_ADC_VSYS);
  168. static SENSOR_DEVICE_ATTR(in0_label, S_IRUGO, show_label, NULL,
  169. DA9055_ADC_VSYS);
  170. static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, da9055_read_auto_ch, NULL,
  171. DA9055_ADC_ADCIN1);
  172. static SENSOR_DEVICE_ATTR(in1_label, S_IRUGO, show_label, NULL,
  173. DA9055_ADC_ADCIN1);
  174. static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, da9055_read_auto_ch, NULL,
  175. DA9055_ADC_ADCIN2);
  176. static SENSOR_DEVICE_ATTR(in2_label, S_IRUGO, show_label, NULL,
  177. DA9055_ADC_ADCIN2);
  178. static SENSOR_DEVICE_ATTR(in3_input, S_IRUGO, da9055_read_auto_ch, NULL,
  179. DA9055_ADC_ADCIN3);
  180. static SENSOR_DEVICE_ATTR(in3_label, S_IRUGO, show_label, NULL,
  181. DA9055_ADC_ADCIN3);
  182. static SENSOR_DEVICE_ATTR(temp1_input, S_IRUGO, da9055_read_tjunc, NULL,
  183. DA9055_ADC_TJUNC);
  184. static SENSOR_DEVICE_ATTR(temp1_label, S_IRUGO, show_label, NULL,
  185. DA9055_ADC_TJUNC);
  186. static struct attribute *da9055_attrs[] = {
  187. &sensor_dev_attr_in0_input.dev_attr.attr,
  188. &sensor_dev_attr_in0_label.dev_attr.attr,
  189. &sensor_dev_attr_in1_input.dev_attr.attr,
  190. &sensor_dev_attr_in1_label.dev_attr.attr,
  191. &sensor_dev_attr_in2_input.dev_attr.attr,
  192. &sensor_dev_attr_in2_label.dev_attr.attr,
  193. &sensor_dev_attr_in3_input.dev_attr.attr,
  194. &sensor_dev_attr_in3_label.dev_attr.attr,
  195. &sensor_dev_attr_temp1_input.dev_attr.attr,
  196. &sensor_dev_attr_temp1_label.dev_attr.attr,
  197. NULL
  198. };
  199. ATTRIBUTE_GROUPS(da9055);
  200. static int da9055_hwmon_probe(struct platform_device *pdev)
  201. {
  202. struct device *dev = &pdev->dev;
  203. struct da9055_hwmon *hwmon;
  204. struct device *hwmon_dev;
  205. int hwmon_irq, ret;
  206. hwmon = devm_kzalloc(dev, sizeof(struct da9055_hwmon), GFP_KERNEL);
  207. if (!hwmon)
  208. return -ENOMEM;
  209. mutex_init(&hwmon->hwmon_lock);
  210. mutex_init(&hwmon->irq_lock);
  211. init_completion(&hwmon->done);
  212. hwmon->da9055 = dev_get_drvdata(pdev->dev.parent);
  213. hwmon_irq = platform_get_irq_byname(pdev, "HWMON");
  214. if (hwmon_irq < 0)
  215. return hwmon_irq;
  216. ret = devm_request_threaded_irq(&pdev->dev, hwmon_irq,
  217. NULL, da9055_auxadc_irq,
  218. IRQF_TRIGGER_HIGH | IRQF_ONESHOT,
  219. "adc-irq", hwmon);
  220. if (ret != 0) {
  221. dev_err(hwmon->da9055->dev, "DA9055 ADC IRQ failed ret=%d\n",
  222. ret);
  223. return ret;
  224. }
  225. hwmon_dev = devm_hwmon_device_register_with_groups(dev, "da9055",
  226. hwmon,
  227. da9055_groups);
  228. return PTR_ERR_OR_ZERO(hwmon_dev);
  229. }
  230. static struct platform_driver da9055_hwmon_driver = {
  231. .probe = da9055_hwmon_probe,
  232. .driver = {
  233. .name = "da9055-hwmon",
  234. },
  235. };
  236. module_platform_driver(da9055_hwmon_driver);
  237. MODULE_AUTHOR("David Dajun Chen <dchen@diasemi.com>");
  238. MODULE_DESCRIPTION("DA9055 HWMON driver");
  239. MODULE_LICENSE("GPL");
  240. MODULE_ALIAS("platform:da9055-hwmon");