pcf8591.c 8.4 KB

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