powr1220.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * powr1220.c - Driver for the Lattice POWR1220 programmable power supply
  4. * and monitor. Users can read all ADC inputs along with their labels
  5. * using the sysfs nodes.
  6. *
  7. * Copyright (c) 2014 Echo360 https://www.echo360.com
  8. * Scott Kanowitz <[email protected]> <[email protected]>
  9. */
  10. #include <linux/module.h>
  11. #include <linux/init.h>
  12. #include <linux/slab.h>
  13. #include <linux/jiffies.h>
  14. #include <linux/i2c.h>
  15. #include <linux/hwmon.h>
  16. #include <linux/hwmon-sysfs.h>
  17. #include <linux/err.h>
  18. #include <linux/mutex.h>
  19. #include <linux/delay.h>
  20. #define ADC_STEP_MV 2
  21. #define ADC_MAX_LOW_MEASUREMENT_MV 2000
  22. enum powr1xxx_chips { powr1014, powr1220 };
  23. enum powr1220_regs {
  24. VMON_STATUS0,
  25. VMON_STATUS1,
  26. VMON_STATUS2,
  27. OUTPUT_STATUS0,
  28. OUTPUT_STATUS1,
  29. OUTPUT_STATUS2,
  30. INPUT_STATUS,
  31. ADC_VALUE_LOW,
  32. ADC_VALUE_HIGH,
  33. ADC_MUX,
  34. UES_BYTE0,
  35. UES_BYTE1,
  36. UES_BYTE2,
  37. UES_BYTE3,
  38. GP_OUTPUT1,
  39. GP_OUTPUT2,
  40. GP_OUTPUT3,
  41. INPUT_VALUE,
  42. RESET,
  43. TRIM1_TRIM,
  44. TRIM2_TRIM,
  45. TRIM3_TRIM,
  46. TRIM4_TRIM,
  47. TRIM5_TRIM,
  48. TRIM6_TRIM,
  49. TRIM7_TRIM,
  50. TRIM8_TRIM,
  51. MAX_POWR1220_REGS
  52. };
  53. enum powr1220_adc_values {
  54. VMON1,
  55. VMON2,
  56. VMON3,
  57. VMON4,
  58. VMON5,
  59. VMON6,
  60. VMON7,
  61. VMON8,
  62. VMON9,
  63. VMON10,
  64. VMON11,
  65. VMON12,
  66. VCCA,
  67. VCCINP,
  68. MAX_POWR1220_ADC_VALUES
  69. };
  70. struct powr1220_data {
  71. struct i2c_client *client;
  72. struct mutex update_lock;
  73. u8 max_channels;
  74. bool adc_valid[MAX_POWR1220_ADC_VALUES];
  75. /* the next value is in jiffies */
  76. unsigned long adc_last_updated[MAX_POWR1220_ADC_VALUES];
  77. /* values */
  78. int adc_maxes[MAX_POWR1220_ADC_VALUES];
  79. int adc_values[MAX_POWR1220_ADC_VALUES];
  80. };
  81. static const char * const input_names[] = {
  82. [VMON1] = "vmon1",
  83. [VMON2] = "vmon2",
  84. [VMON3] = "vmon3",
  85. [VMON4] = "vmon4",
  86. [VMON5] = "vmon5",
  87. [VMON6] = "vmon6",
  88. [VMON7] = "vmon7",
  89. [VMON8] = "vmon8",
  90. [VMON9] = "vmon9",
  91. [VMON10] = "vmon10",
  92. [VMON11] = "vmon11",
  93. [VMON12] = "vmon12",
  94. [VCCA] = "vcca",
  95. [VCCINP] = "vccinp",
  96. };
  97. /* Reads the specified ADC channel */
  98. static int powr1220_read_adc(struct device *dev, int ch_num)
  99. {
  100. struct powr1220_data *data = dev_get_drvdata(dev);
  101. int reading;
  102. int result;
  103. int adc_range = 0;
  104. mutex_lock(&data->update_lock);
  105. if (time_after(jiffies, data->adc_last_updated[ch_num] + HZ) ||
  106. !data->adc_valid[ch_num]) {
  107. /*
  108. * figure out if we need to use the attenuator for
  109. * high inputs or inputs that we don't yet have a measurement
  110. * for. We dynamically set the attenuator depending on the
  111. * max reading.
  112. */
  113. if (data->adc_maxes[ch_num] > ADC_MAX_LOW_MEASUREMENT_MV ||
  114. data->adc_maxes[ch_num] == 0)
  115. adc_range = 1 << 4;
  116. /* set the attenuator and mux */
  117. result = i2c_smbus_write_byte_data(data->client, ADC_MUX,
  118. adc_range | ch_num);
  119. if (result)
  120. goto exit;
  121. /*
  122. * wait at least Tconvert time (200 us) for the
  123. * conversion to complete
  124. */
  125. udelay(200);
  126. /* get the ADC reading */
  127. result = i2c_smbus_read_byte_data(data->client, ADC_VALUE_LOW);
  128. if (result < 0)
  129. goto exit;
  130. reading = result >> 4;
  131. /* get the upper half of the reading */
  132. result = i2c_smbus_read_byte_data(data->client, ADC_VALUE_HIGH);
  133. if (result < 0)
  134. goto exit;
  135. reading |= result << 4;
  136. /* now convert the reading to a voltage */
  137. reading *= ADC_STEP_MV;
  138. data->adc_values[ch_num] = reading;
  139. data->adc_valid[ch_num] = true;
  140. data->adc_last_updated[ch_num] = jiffies;
  141. result = reading;
  142. if (reading > data->adc_maxes[ch_num])
  143. data->adc_maxes[ch_num] = reading;
  144. } else {
  145. result = data->adc_values[ch_num];
  146. }
  147. exit:
  148. mutex_unlock(&data->update_lock);
  149. return result;
  150. }
  151. static umode_t
  152. powr1220_is_visible(const void *data, enum hwmon_sensor_types type, u32
  153. attr, int channel)
  154. {
  155. struct powr1220_data *chip_data = (struct powr1220_data *)data;
  156. if (channel >= chip_data->max_channels)
  157. return 0;
  158. switch (type) {
  159. case hwmon_in:
  160. switch (attr) {
  161. case hwmon_in_input:
  162. case hwmon_in_highest:
  163. case hwmon_in_label:
  164. return 0444;
  165. default:
  166. break;
  167. }
  168. break;
  169. default:
  170. break;
  171. }
  172. return 0;
  173. }
  174. static int
  175. powr1220_read_string(struct device *dev, enum hwmon_sensor_types type, u32 attr,
  176. int channel, const char **str)
  177. {
  178. switch (type) {
  179. case hwmon_in:
  180. switch (attr) {
  181. case hwmon_in_label:
  182. *str = input_names[channel];
  183. return 0;
  184. default:
  185. return -EOPNOTSUPP;
  186. }
  187. break;
  188. default:
  189. return -EOPNOTSUPP;
  190. }
  191. return -EOPNOTSUPP;
  192. }
  193. static int
  194. powr1220_read(struct device *dev, enum hwmon_sensor_types type, u32
  195. attr, int channel, long *val)
  196. {
  197. struct powr1220_data *data = dev_get_drvdata(dev);
  198. int ret;
  199. switch (type) {
  200. case hwmon_in:
  201. switch (attr) {
  202. case hwmon_in_input:
  203. ret = powr1220_read_adc(dev, channel);
  204. if (ret < 0)
  205. return ret;
  206. *val = ret;
  207. break;
  208. case hwmon_in_highest:
  209. *val = data->adc_maxes[channel];
  210. break;
  211. default:
  212. return -EOPNOTSUPP;
  213. }
  214. break;
  215. default:
  216. return -EOPNOTSUPP;
  217. }
  218. return 0;
  219. }
  220. static const struct hwmon_channel_info *powr1220_info[] = {
  221. HWMON_CHANNEL_INFO(in,
  222. HWMON_I_INPUT | HWMON_I_HIGHEST | HWMON_I_LABEL,
  223. HWMON_I_INPUT | HWMON_I_HIGHEST | HWMON_I_LABEL,
  224. HWMON_I_INPUT | HWMON_I_HIGHEST | HWMON_I_LABEL,
  225. HWMON_I_INPUT | HWMON_I_HIGHEST | HWMON_I_LABEL,
  226. HWMON_I_INPUT | HWMON_I_HIGHEST | HWMON_I_LABEL,
  227. HWMON_I_INPUT | HWMON_I_HIGHEST | HWMON_I_LABEL,
  228. HWMON_I_INPUT | HWMON_I_HIGHEST | HWMON_I_LABEL,
  229. HWMON_I_INPUT | HWMON_I_HIGHEST | HWMON_I_LABEL,
  230. HWMON_I_INPUT | HWMON_I_HIGHEST | HWMON_I_LABEL,
  231. HWMON_I_INPUT | HWMON_I_HIGHEST | HWMON_I_LABEL,
  232. HWMON_I_INPUT | HWMON_I_HIGHEST | HWMON_I_LABEL,
  233. HWMON_I_INPUT | HWMON_I_HIGHEST | HWMON_I_LABEL,
  234. HWMON_I_INPUT | HWMON_I_HIGHEST | HWMON_I_LABEL,
  235. HWMON_I_INPUT | HWMON_I_HIGHEST | HWMON_I_LABEL),
  236. NULL
  237. };
  238. static const struct hwmon_ops powr1220_hwmon_ops = {
  239. .read = powr1220_read,
  240. .read_string = powr1220_read_string,
  241. .is_visible = powr1220_is_visible,
  242. };
  243. static const struct hwmon_chip_info powr1220_chip_info = {
  244. .ops = &powr1220_hwmon_ops,
  245. .info = powr1220_info,
  246. };
  247. static const struct i2c_device_id powr1220_ids[];
  248. static int powr1220_probe(struct i2c_client *client)
  249. {
  250. struct powr1220_data *data;
  251. struct device *hwmon_dev;
  252. if (!i2c_check_functionality(client->adapter, I2C_FUNC_SMBUS_BYTE_DATA))
  253. return -ENODEV;
  254. data = devm_kzalloc(&client->dev, sizeof(*data), GFP_KERNEL);
  255. if (!data)
  256. return -ENOMEM;
  257. switch (i2c_match_id(powr1220_ids, client)->driver_data) {
  258. case powr1014:
  259. data->max_channels = 10;
  260. break;
  261. default:
  262. data->max_channels = 12;
  263. break;
  264. }
  265. mutex_init(&data->update_lock);
  266. data->client = client;
  267. hwmon_dev = devm_hwmon_device_register_with_info(&client->dev,
  268. client->name,
  269. data,
  270. &powr1220_chip_info,
  271. NULL);
  272. return PTR_ERR_OR_ZERO(hwmon_dev);
  273. }
  274. static const struct i2c_device_id powr1220_ids[] = {
  275. { "powr1014", powr1014, },
  276. { "powr1220", powr1220, },
  277. { }
  278. };
  279. MODULE_DEVICE_TABLE(i2c, powr1220_ids);
  280. static struct i2c_driver powr1220_driver = {
  281. .class = I2C_CLASS_HWMON,
  282. .driver = {
  283. .name = "powr1220",
  284. },
  285. .probe_new = powr1220_probe,
  286. .id_table = powr1220_ids,
  287. };
  288. module_i2c_driver(powr1220_driver);
  289. MODULE_AUTHOR("Scott Kanowitz");
  290. MODULE_DESCRIPTION("POWR1220 driver");
  291. MODULE_LICENSE("GPL");