powr1220.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. /*
  2. * powr1220.c - Driver for the Lattice POWR1220 programmable power supply
  3. * and monitor. Users can read all ADC inputs along with their labels
  4. * using the sysfs nodes.
  5. *
  6. * Copyright (c) 2014 Echo360 http://www.echo360.com
  7. * Scott Kanowitz <skanowitz@echo360.com> <scott.kanowitz@gmail.com>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. */
  19. #include <linux/module.h>
  20. #include <linux/init.h>
  21. #include <linux/slab.h>
  22. #include <linux/jiffies.h>
  23. #include <linux/i2c.h>
  24. #include <linux/hwmon.h>
  25. #include <linux/hwmon-sysfs.h>
  26. #include <linux/err.h>
  27. #include <linux/mutex.h>
  28. #include <linux/delay.h>
  29. #define ADC_STEP_MV 2
  30. #define ADC_MAX_LOW_MEASUREMENT_MV 2000
  31. enum powr1220_regs {
  32. VMON_STATUS0,
  33. VMON_STATUS1,
  34. VMON_STATUS2,
  35. OUTPUT_STATUS0,
  36. OUTPUT_STATUS1,
  37. OUTPUT_STATUS2,
  38. INPUT_STATUS,
  39. ADC_VALUE_LOW,
  40. ADC_VALUE_HIGH,
  41. ADC_MUX,
  42. UES_BYTE0,
  43. UES_BYTE1,
  44. UES_BYTE2,
  45. UES_BYTE3,
  46. GP_OUTPUT1,
  47. GP_OUTPUT2,
  48. GP_OUTPUT3,
  49. INPUT_VALUE,
  50. RESET,
  51. TRIM1_TRIM,
  52. TRIM2_TRIM,
  53. TRIM3_TRIM,
  54. TRIM4_TRIM,
  55. TRIM5_TRIM,
  56. TRIM6_TRIM,
  57. TRIM7_TRIM,
  58. TRIM8_TRIM,
  59. MAX_POWR1220_REGS
  60. };
  61. enum powr1220_adc_values {
  62. VMON1,
  63. VMON2,
  64. VMON3,
  65. VMON4,
  66. VMON5,
  67. VMON6,
  68. VMON7,
  69. VMON8,
  70. VMON9,
  71. VMON10,
  72. VMON11,
  73. VMON12,
  74. VCCA,
  75. VCCINP,
  76. MAX_POWR1220_ADC_VALUES
  77. };
  78. struct powr1220_data {
  79. struct i2c_client *client;
  80. struct mutex update_lock;
  81. bool adc_valid[MAX_POWR1220_ADC_VALUES];
  82. /* the next value is in jiffies */
  83. unsigned long adc_last_updated[MAX_POWR1220_ADC_VALUES];
  84. /* values */
  85. int adc_maxes[MAX_POWR1220_ADC_VALUES];
  86. int adc_values[MAX_POWR1220_ADC_VALUES];
  87. };
  88. static const char * const input_names[] = {
  89. [VMON1] = "vmon1",
  90. [VMON2] = "vmon2",
  91. [VMON3] = "vmon3",
  92. [VMON4] = "vmon4",
  93. [VMON5] = "vmon5",
  94. [VMON6] = "vmon6",
  95. [VMON7] = "vmon7",
  96. [VMON8] = "vmon8",
  97. [VMON9] = "vmon9",
  98. [VMON10] = "vmon10",
  99. [VMON11] = "vmon11",
  100. [VMON12] = "vmon12",
  101. [VCCA] = "vcca",
  102. [VCCINP] = "vccinp",
  103. };
  104. /* Reads the specified ADC channel */
  105. static int powr1220_read_adc(struct device *dev, int ch_num)
  106. {
  107. struct powr1220_data *data = dev_get_drvdata(dev);
  108. int reading;
  109. int result;
  110. int adc_range = 0;
  111. mutex_lock(&data->update_lock);
  112. if (time_after(jiffies, data->adc_last_updated[ch_num] + HZ) ||
  113. !data->adc_valid[ch_num]) {
  114. /*
  115. * figure out if we need to use the attenuator for
  116. * high inputs or inputs that we don't yet have a measurement
  117. * for. We dynamically set the attenuator depending on the
  118. * max reading.
  119. */
  120. if (data->adc_maxes[ch_num] > ADC_MAX_LOW_MEASUREMENT_MV ||
  121. data->adc_maxes[ch_num] == 0)
  122. adc_range = 1 << 4;
  123. /* set the attenuator and mux */
  124. result = i2c_smbus_write_byte_data(data->client, ADC_MUX,
  125. adc_range | ch_num);
  126. if (result)
  127. goto exit;
  128. /*
  129. * wait at least Tconvert time (200 us) for the
  130. * conversion to complete
  131. */
  132. udelay(200);
  133. /* get the ADC reading */
  134. result = i2c_smbus_read_byte_data(data->client, ADC_VALUE_LOW);
  135. if (result < 0)
  136. goto exit;
  137. reading = result >> 4;
  138. /* get the upper half of the reading */
  139. result = i2c_smbus_read_byte_data(data->client, ADC_VALUE_HIGH);
  140. if (result < 0)
  141. goto exit;
  142. reading |= result << 4;
  143. /* now convert the reading to a voltage */
  144. reading *= ADC_STEP_MV;
  145. data->adc_values[ch_num] = reading;
  146. data->adc_valid[ch_num] = true;
  147. data->adc_last_updated[ch_num] = jiffies;
  148. result = reading;
  149. if (reading > data->adc_maxes[ch_num])
  150. data->adc_maxes[ch_num] = reading;
  151. } else {
  152. result = data->adc_values[ch_num];
  153. }
  154. exit:
  155. mutex_unlock(&data->update_lock);
  156. return result;
  157. }
  158. /* Shows the voltage associated with the specified ADC channel */
  159. static ssize_t powr1220_show_voltage(struct device *dev,
  160. struct device_attribute *dev_attr, char *buf)
  161. {
  162. struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr);
  163. int adc_val = powr1220_read_adc(dev, attr->index);
  164. if (adc_val < 0)
  165. return adc_val;
  166. return sprintf(buf, "%d\n", adc_val);
  167. }
  168. /* Shows the maximum setting associated with the specified ADC channel */
  169. static ssize_t powr1220_show_max(struct device *dev,
  170. struct device_attribute *dev_attr, char *buf)
  171. {
  172. struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr);
  173. struct powr1220_data *data = dev_get_drvdata(dev);
  174. return sprintf(buf, "%d\n", data->adc_maxes[attr->index]);
  175. }
  176. /* Shows the label associated with the specified ADC channel */
  177. static ssize_t powr1220_show_label(struct device *dev,
  178. struct device_attribute *dev_attr, char *buf)
  179. {
  180. struct sensor_device_attribute *attr = to_sensor_dev_attr(dev_attr);
  181. return sprintf(buf, "%s\n", input_names[attr->index]);
  182. }
  183. static SENSOR_DEVICE_ATTR(in0_input, S_IRUGO, powr1220_show_voltage, NULL,
  184. VMON1);
  185. static SENSOR_DEVICE_ATTR(in1_input, S_IRUGO, powr1220_show_voltage, NULL,
  186. VMON2);
  187. static SENSOR_DEVICE_ATTR(in2_input, S_IRUGO, powr1220_show_voltage, NULL,
  188. VMON3);
  189. static SENSOR_DEVICE_ATTR(in3_input, S_IRUGO, powr1220_show_voltage, NULL,
  190. VMON4);
  191. static SENSOR_DEVICE_ATTR(in4_input, S_IRUGO, powr1220_show_voltage, NULL,
  192. VMON5);
  193. static SENSOR_DEVICE_ATTR(in5_input, S_IRUGO, powr1220_show_voltage, NULL,
  194. VMON6);
  195. static SENSOR_DEVICE_ATTR(in6_input, S_IRUGO, powr1220_show_voltage, NULL,
  196. VMON7);
  197. static SENSOR_DEVICE_ATTR(in7_input, S_IRUGO, powr1220_show_voltage, NULL,
  198. VMON8);
  199. static SENSOR_DEVICE_ATTR(in8_input, S_IRUGO, powr1220_show_voltage, NULL,
  200. VMON9);
  201. static SENSOR_DEVICE_ATTR(in9_input, S_IRUGO, powr1220_show_voltage, NULL,
  202. VMON10);
  203. static SENSOR_DEVICE_ATTR(in10_input, S_IRUGO, powr1220_show_voltage, NULL,
  204. VMON11);
  205. static SENSOR_DEVICE_ATTR(in11_input, S_IRUGO, powr1220_show_voltage, NULL,
  206. VMON12);
  207. static SENSOR_DEVICE_ATTR(in12_input, S_IRUGO, powr1220_show_voltage, NULL,
  208. VCCA);
  209. static SENSOR_DEVICE_ATTR(in13_input, S_IRUGO, powr1220_show_voltage, NULL,
  210. VCCINP);
  211. static SENSOR_DEVICE_ATTR(in0_highest, S_IRUGO, powr1220_show_max, NULL,
  212. VMON1);
  213. static SENSOR_DEVICE_ATTR(in1_highest, S_IRUGO, powr1220_show_max, NULL,
  214. VMON2);
  215. static SENSOR_DEVICE_ATTR(in2_highest, S_IRUGO, powr1220_show_max, NULL,
  216. VMON3);
  217. static SENSOR_DEVICE_ATTR(in3_highest, S_IRUGO, powr1220_show_max, NULL,
  218. VMON4);
  219. static SENSOR_DEVICE_ATTR(in4_highest, S_IRUGO, powr1220_show_max, NULL,
  220. VMON5);
  221. static SENSOR_DEVICE_ATTR(in5_highest, S_IRUGO, powr1220_show_max, NULL,
  222. VMON6);
  223. static SENSOR_DEVICE_ATTR(in6_highest, S_IRUGO, powr1220_show_max, NULL,
  224. VMON7);
  225. static SENSOR_DEVICE_ATTR(in7_highest, S_IRUGO, powr1220_show_max, NULL,
  226. VMON8);
  227. static SENSOR_DEVICE_ATTR(in8_highest, S_IRUGO, powr1220_show_max, NULL,
  228. VMON9);
  229. static SENSOR_DEVICE_ATTR(in9_highest, S_IRUGO, powr1220_show_max, NULL,
  230. VMON10);
  231. static SENSOR_DEVICE_ATTR(in10_highest, S_IRUGO, powr1220_show_max, NULL,
  232. VMON11);
  233. static SENSOR_DEVICE_ATTR(in11_highest, S_IRUGO, powr1220_show_max, NULL,
  234. VMON12);
  235. static SENSOR_DEVICE_ATTR(in12_highest, S_IRUGO, powr1220_show_max, NULL,
  236. VCCA);
  237. static SENSOR_DEVICE_ATTR(in13_highest, S_IRUGO, powr1220_show_max, NULL,
  238. VCCINP);
  239. static SENSOR_DEVICE_ATTR(in0_label, S_IRUGO, powr1220_show_label, NULL,
  240. VMON1);
  241. static SENSOR_DEVICE_ATTR(in1_label, S_IRUGO, powr1220_show_label, NULL,
  242. VMON2);
  243. static SENSOR_DEVICE_ATTR(in2_label, S_IRUGO, powr1220_show_label, NULL,
  244. VMON3);
  245. static SENSOR_DEVICE_ATTR(in3_label, S_IRUGO, powr1220_show_label, NULL,
  246. VMON4);
  247. static SENSOR_DEVICE_ATTR(in4_label, S_IRUGO, powr1220_show_label, NULL,
  248. VMON5);
  249. static SENSOR_DEVICE_ATTR(in5_label, S_IRUGO, powr1220_show_label, NULL,
  250. VMON6);
  251. static SENSOR_DEVICE_ATTR(in6_label, S_IRUGO, powr1220_show_label, NULL,
  252. VMON7);
  253. static SENSOR_DEVICE_ATTR(in7_label, S_IRUGO, powr1220_show_label, NULL,
  254. VMON8);
  255. static SENSOR_DEVICE_ATTR(in8_label, S_IRUGO, powr1220_show_label, NULL,
  256. VMON9);
  257. static SENSOR_DEVICE_ATTR(in9_label, S_IRUGO, powr1220_show_label, NULL,
  258. VMON10);
  259. static SENSOR_DEVICE_ATTR(in10_label, S_IRUGO, powr1220_show_label, NULL,
  260. VMON11);
  261. static SENSOR_DEVICE_ATTR(in11_label, S_IRUGO, powr1220_show_label, NULL,
  262. VMON12);
  263. static SENSOR_DEVICE_ATTR(in12_label, S_IRUGO, powr1220_show_label, NULL,
  264. VCCA);
  265. static SENSOR_DEVICE_ATTR(in13_label, S_IRUGO, powr1220_show_label, NULL,
  266. VCCINP);
  267. static struct attribute *powr1220_attrs[] = {
  268. &sensor_dev_attr_in0_input.dev_attr.attr,
  269. &sensor_dev_attr_in1_input.dev_attr.attr,
  270. &sensor_dev_attr_in2_input.dev_attr.attr,
  271. &sensor_dev_attr_in3_input.dev_attr.attr,
  272. &sensor_dev_attr_in4_input.dev_attr.attr,
  273. &sensor_dev_attr_in5_input.dev_attr.attr,
  274. &sensor_dev_attr_in6_input.dev_attr.attr,
  275. &sensor_dev_attr_in7_input.dev_attr.attr,
  276. &sensor_dev_attr_in8_input.dev_attr.attr,
  277. &sensor_dev_attr_in9_input.dev_attr.attr,
  278. &sensor_dev_attr_in10_input.dev_attr.attr,
  279. &sensor_dev_attr_in11_input.dev_attr.attr,
  280. &sensor_dev_attr_in12_input.dev_attr.attr,
  281. &sensor_dev_attr_in13_input.dev_attr.attr,
  282. &sensor_dev_attr_in0_highest.dev_attr.attr,
  283. &sensor_dev_attr_in1_highest.dev_attr.attr,
  284. &sensor_dev_attr_in2_highest.dev_attr.attr,
  285. &sensor_dev_attr_in3_highest.dev_attr.attr,
  286. &sensor_dev_attr_in4_highest.dev_attr.attr,
  287. &sensor_dev_attr_in5_highest.dev_attr.attr,
  288. &sensor_dev_attr_in6_highest.dev_attr.attr,
  289. &sensor_dev_attr_in7_highest.dev_attr.attr,
  290. &sensor_dev_attr_in8_highest.dev_attr.attr,
  291. &sensor_dev_attr_in9_highest.dev_attr.attr,
  292. &sensor_dev_attr_in10_highest.dev_attr.attr,
  293. &sensor_dev_attr_in11_highest.dev_attr.attr,
  294. &sensor_dev_attr_in12_highest.dev_attr.attr,
  295. &sensor_dev_attr_in13_highest.dev_attr.attr,
  296. &sensor_dev_attr_in0_label.dev_attr.attr,
  297. &sensor_dev_attr_in1_label.dev_attr.attr,
  298. &sensor_dev_attr_in2_label.dev_attr.attr,
  299. &sensor_dev_attr_in3_label.dev_attr.attr,
  300. &sensor_dev_attr_in4_label.dev_attr.attr,
  301. &sensor_dev_attr_in5_label.dev_attr.attr,
  302. &sensor_dev_attr_in6_label.dev_attr.attr,
  303. &sensor_dev_attr_in7_label.dev_attr.attr,
  304. &sensor_dev_attr_in8_label.dev_attr.attr,
  305. &sensor_dev_attr_in9_label.dev_attr.attr,
  306. &sensor_dev_attr_in10_label.dev_attr.attr,
  307. &sensor_dev_attr_in11_label.dev_attr.attr,
  308. &sensor_dev_attr_in12_label.dev_attr.attr,
  309. &sensor_dev_attr_in13_label.dev_attr.attr,
  310. NULL
  311. };
  312. ATTRIBUTE_GROUPS(powr1220);
  313. static int powr1220_probe(struct i2c_client *client,
  314. const struct i2c_device_id *id)
  315. {
  316. struct powr1220_data *data;
  317. struct device *hwmon_dev;
  318. if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  319. return -ENODEV;
  320. data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL);
  321. if (!data)
  322. return -ENOMEM;
  323. mutex_init(&data->update_lock);
  324. data->client = client;
  325. hwmon_dev = devm_hwmon_device_register_with_groups(&client->dev,
  326. client->name, data, powr1220_groups);
  327. return PTR_ERR_OR_ZERO(hwmon_dev);
  328. }
  329. static const struct i2c_device_id powr1220_ids[] = {
  330. { "powr1220", 0, },
  331. { }
  332. };
  333. MODULE_DEVICE_TABLE(i2c, powr1220_ids);
  334. static struct i2c_driver powr1220_driver = {
  335. .class = I2C_CLASS_HWMON,
  336. .driver = {
  337. .name = "powr1220",
  338. },
  339. .probe = powr1220_probe,
  340. .id_table = powr1220_ids,
  341. };
  342. module_i2c_driver(powr1220_driver);
  343. MODULE_AUTHOR("Scott Kanowitz");
  344. MODULE_DESCRIPTION("POWR1220 driver");
  345. MODULE_LICENSE("GPL");