pcf8591.c 7.7 KB

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