pcf8591.c 8.4 KB

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