pcf8591.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2001-2004 Aurelien Jarno <[email protected]>
  4. * Ported to Linux 2.6 by Aurelien Jarno <[email protected]> with
  5. * the help of Jean Delvare <[email protected]>
  6. */
  7. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  8. #include <linux/module.h>
  9. #include <linux/init.h>
  10. #include <linux/slab.h>
  11. #include <linux/i2c.h>
  12. #include <linux/mutex.h>
  13. #include <linux/err.h>
  14. #include <linux/hwmon.h>
  15. /* Insmod parameters */
  16. static int input_mode;
  17. module_param(input_mode, int, 0);
  18. MODULE_PARM_DESC(input_mode,
  19. "Analog input mode:\n"
  20. " 0 = four single ended inputs\n"
  21. " 1 = three differential inputs\n"
  22. " 2 = single ended and differential mixed\n"
  23. " 3 = two differential inputs\n");
  24. /*
  25. * The PCF8591 control byte
  26. * 7 6 5 4 3 2 1 0
  27. * | 0 |AOEF| AIP | 0 |AINC| AICH |
  28. */
  29. /* Analog Output Enable Flag (analog output active if 1) */
  30. #define PCF8591_CONTROL_AOEF 0x40
  31. /*
  32. * Analog Input Programming
  33. * 0x00 = four single ended inputs
  34. * 0x10 = three differential inputs
  35. * 0x20 = single ended and differential mixed
  36. * 0x30 = two differential inputs
  37. */
  38. #define PCF8591_CONTROL_AIP_MASK 0x30
  39. /* Autoincrement Flag (switch on if 1) */
  40. #define PCF8591_CONTROL_AINC 0x04
  41. /*
  42. * Channel selection
  43. * 0x00 = channel 0
  44. * 0x01 = channel 1
  45. * 0x02 = channel 2
  46. * 0x03 = channel 3
  47. */
  48. #define PCF8591_CONTROL_AICH_MASK 0x03
  49. /* Initial values */
  50. #define PCF8591_INIT_CONTROL ((input_mode << 4) | PCF8591_CONTROL_AOEF)
  51. #define PCF8591_INIT_AOUT 0 /* DAC out = 0 */
  52. /* Conversions */
  53. #define REG_TO_SIGNED(reg) (((reg) & 0x80) ? ((reg) - 256) : (reg))
  54. struct pcf8591_data {
  55. struct device *hwmon_dev;
  56. struct mutex update_lock;
  57. u8 control;
  58. u8 aout;
  59. };
  60. static void pcf8591_init_client(struct i2c_client *client);
  61. static int pcf8591_read_channel(struct device *dev, int channel);
  62. /* following are the sysfs callback functions */
  63. #define show_in_channel(channel) \
  64. static ssize_t show_in##channel##_input(struct device *dev, \
  65. struct device_attribute *attr, \
  66. char *buf) \
  67. { \
  68. return sprintf(buf, "%d\n", pcf8591_read_channel(dev, channel));\
  69. } \
  70. static DEVICE_ATTR(in##channel##_input, S_IRUGO, \
  71. show_in##channel##_input, NULL);
  72. show_in_channel(0);
  73. show_in_channel(1);
  74. show_in_channel(2);
  75. show_in_channel(3);
  76. static ssize_t out0_output_show(struct device *dev,
  77. struct device_attribute *attr, char *buf)
  78. {
  79. struct pcf8591_data *data = i2c_get_clientdata(to_i2c_client(dev));
  80. return sprintf(buf, "%d\n", data->aout * 10);
  81. }
  82. static ssize_t out0_output_store(struct device *dev,
  83. struct device_attribute *attr,
  84. const char *buf, size_t count)
  85. {
  86. unsigned long val;
  87. struct i2c_client *client = to_i2c_client(dev);
  88. struct pcf8591_data *data = i2c_get_clientdata(client);
  89. int err;
  90. err = kstrtoul(buf, 10, &val);
  91. if (err)
  92. return err;
  93. val /= 10;
  94. if (val > 255)
  95. return -EINVAL;
  96. data->aout = val;
  97. i2c_smbus_write_byte_data(client, data->control, data->aout);
  98. return count;
  99. }
  100. static DEVICE_ATTR_RW(out0_output);
  101. static ssize_t out0_enable_show(struct device *dev,
  102. struct device_attribute *attr, char *buf)
  103. {
  104. struct pcf8591_data *data = i2c_get_clientdata(to_i2c_client(dev));
  105. return sprintf(buf, "%u\n", !(!(data->control & PCF8591_CONTROL_AOEF)));
  106. }
  107. static ssize_t out0_enable_store(struct device *dev,
  108. struct device_attribute *attr,
  109. const char *buf, size_t count)
  110. {
  111. struct i2c_client *client = to_i2c_client(dev);
  112. struct pcf8591_data *data = i2c_get_clientdata(client);
  113. unsigned long val;
  114. int err;
  115. err = kstrtoul(buf, 10, &val);
  116. if (err)
  117. return err;
  118. mutex_lock(&data->update_lock);
  119. if (val)
  120. data->control |= PCF8591_CONTROL_AOEF;
  121. else
  122. data->control &= ~PCF8591_CONTROL_AOEF;
  123. i2c_smbus_write_byte(client, data->control);
  124. mutex_unlock(&data->update_lock);
  125. return count;
  126. }
  127. static DEVICE_ATTR_RW(out0_enable);
  128. static struct attribute *pcf8591_attributes[] = {
  129. &dev_attr_out0_enable.attr,
  130. &dev_attr_out0_output.attr,
  131. &dev_attr_in0_input.attr,
  132. &dev_attr_in1_input.attr,
  133. NULL
  134. };
  135. static const struct attribute_group pcf8591_attr_group = {
  136. .attrs = pcf8591_attributes,
  137. };
  138. static struct attribute *pcf8591_attributes_opt[] = {
  139. &dev_attr_in2_input.attr,
  140. &dev_attr_in3_input.attr,
  141. NULL
  142. };
  143. static const struct attribute_group pcf8591_attr_group_opt = {
  144. .attrs = pcf8591_attributes_opt,
  145. };
  146. /*
  147. * Real code
  148. */
  149. static int pcf8591_probe(struct i2c_client *client)
  150. {
  151. struct pcf8591_data *data;
  152. int err;
  153. data = devm_kzalloc(&client->dev, sizeof(struct pcf8591_data),
  154. GFP_KERNEL);
  155. if (!data)
  156. return -ENOMEM;
  157. i2c_set_clientdata(client, data);
  158. mutex_init(&data->update_lock);
  159. /* Initialize the PCF8591 chip */
  160. pcf8591_init_client(client);
  161. /* Register sysfs hooks */
  162. err = sysfs_create_group(&client->dev.kobj, &pcf8591_attr_group);
  163. if (err)
  164. return err;
  165. /* Register input2 if not in "two differential inputs" mode */
  166. if (input_mode != 3) {
  167. err = device_create_file(&client->dev, &dev_attr_in2_input);
  168. if (err)
  169. goto exit_sysfs_remove;
  170. }
  171. /* Register input3 only in "four single ended inputs" mode */
  172. if (input_mode == 0) {
  173. err = device_create_file(&client->dev, &dev_attr_in3_input);
  174. if (err)
  175. goto exit_sysfs_remove;
  176. }
  177. data->hwmon_dev = hwmon_device_register(&client->dev);
  178. if (IS_ERR(data->hwmon_dev)) {
  179. err = PTR_ERR(data->hwmon_dev);
  180. goto exit_sysfs_remove;
  181. }
  182. return 0;
  183. exit_sysfs_remove:
  184. sysfs_remove_group(&client->dev.kobj, &pcf8591_attr_group_opt);
  185. sysfs_remove_group(&client->dev.kobj, &pcf8591_attr_group);
  186. return err;
  187. }
  188. static void pcf8591_remove(struct i2c_client *client)
  189. {
  190. struct pcf8591_data *data = i2c_get_clientdata(client);
  191. hwmon_device_unregister(data->hwmon_dev);
  192. sysfs_remove_group(&client->dev.kobj, &pcf8591_attr_group_opt);
  193. sysfs_remove_group(&client->dev.kobj, &pcf8591_attr_group);
  194. }
  195. /* Called when we have found a new PCF8591. */
  196. static void pcf8591_init_client(struct i2c_client *client)
  197. {
  198. struct pcf8591_data *data = i2c_get_clientdata(client);
  199. data->control = PCF8591_INIT_CONTROL;
  200. data->aout = PCF8591_INIT_AOUT;
  201. i2c_smbus_write_byte_data(client, data->control, data->aout);
  202. /*
  203. * The first byte transmitted contains the conversion code of the
  204. * previous read cycle. FLUSH IT!
  205. */
  206. i2c_smbus_read_byte(client);
  207. }
  208. static int pcf8591_read_channel(struct device *dev, int channel)
  209. {
  210. u8 value;
  211. struct i2c_client *client = to_i2c_client(dev);
  212. struct pcf8591_data *data = i2c_get_clientdata(client);
  213. mutex_lock(&data->update_lock);
  214. if ((data->control & PCF8591_CONTROL_AICH_MASK) != channel) {
  215. data->control = (data->control & ~PCF8591_CONTROL_AICH_MASK)
  216. | channel;
  217. i2c_smbus_write_byte(client, data->control);
  218. /*
  219. * The first byte transmitted contains the conversion code of
  220. * the previous read cycle. FLUSH IT!
  221. */
  222. i2c_smbus_read_byte(client);
  223. }
  224. value = i2c_smbus_read_byte(client);
  225. mutex_unlock(&data->update_lock);
  226. if ((channel == 2 && input_mode == 2) ||
  227. (channel != 3 && (input_mode == 1 || input_mode == 3)))
  228. return 10 * REG_TO_SIGNED(value);
  229. else
  230. return 10 * value;
  231. }
  232. static const struct i2c_device_id pcf8591_id[] = {
  233. { "pcf8591", 0 },
  234. { }
  235. };
  236. MODULE_DEVICE_TABLE(i2c, pcf8591_id);
  237. static struct i2c_driver pcf8591_driver = {
  238. .driver = {
  239. .name = "pcf8591",
  240. },
  241. .probe_new = pcf8591_probe,
  242. .remove = pcf8591_remove,
  243. .id_table = pcf8591_id,
  244. };
  245. static int __init pcf8591_init(void)
  246. {
  247. if (input_mode < 0 || input_mode > 3) {
  248. pr_warn("invalid input_mode (%d)\n", input_mode);
  249. input_mode = 0;
  250. }
  251. return i2c_add_driver(&pcf8591_driver);
  252. }
  253. static void __exit pcf8591_exit(void)
  254. {
  255. i2c_del_driver(&pcf8591_driver);
  256. }
  257. MODULE_AUTHOR("Aurelien Jarno <[email protected]>");
  258. MODULE_DESCRIPTION("PCF8591 driver");
  259. MODULE_LICENSE("GPL");
  260. module_init(pcf8591_init);
  261. module_exit(pcf8591_exit);