pcf8591.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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. /* Addresses to scan */
  23. static const unsigned short normal_i2c[] = { 0x48, 0x49, 0x4a, 0x4b, 0x4c,
  24. 0x4d, 0x4e, 0x4f, I2C_CLIENT_END };
  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 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. /* Return 0 if detection is successful, -ENODEV otherwise */
  139. static int pcf8591_detect(struct i2c_client *client,
  140. struct i2c_board_info *info)
  141. {
  142. struct i2c_adapter *adapter = client->adapter;
  143. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE
  144. | I2C_FUNC_SMBUS_WRITE_BYTE_DATA))
  145. return -ENODEV;
  146. /* Now, we would do the remaining detection. But the PCF8591 is plainly
  147. impossible to detect! Stupid chip. */
  148. strlcpy(info->type, "pcf8591", I2C_NAME_SIZE);
  149. return 0;
  150. }
  151. static int pcf8591_probe(struct i2c_client *client,
  152. const struct i2c_device_id *id)
  153. {
  154. struct pcf8591_data *data;
  155. int err;
  156. if (!(data = kzalloc(sizeof(struct pcf8591_data), GFP_KERNEL))) {
  157. err = -ENOMEM;
  158. goto exit;
  159. }
  160. i2c_set_clientdata(client, data);
  161. mutex_init(&data->update_lock);
  162. /* Initialize the PCF8591 chip */
  163. pcf8591_init_client(client);
  164. /* Register sysfs hooks */
  165. err = sysfs_create_group(&client->dev.kobj, &pcf8591_attr_group);
  166. if (err)
  167. goto exit_kfree;
  168. /* Register input2 if not in "two differential inputs" mode */
  169. if (input_mode != 3) {
  170. if ((err = device_create_file(&client->dev,
  171. &dev_attr_in2_input)))
  172. goto exit_sysfs_remove;
  173. }
  174. /* Register input3 only in "four single ended inputs" mode */
  175. if (input_mode == 0) {
  176. if ((err = device_create_file(&client->dev,
  177. &dev_attr_in3_input)))
  178. goto exit_sysfs_remove;
  179. }
  180. return 0;
  181. exit_sysfs_remove:
  182. sysfs_remove_group(&client->dev.kobj, &pcf8591_attr_group_opt);
  183. sysfs_remove_group(&client->dev.kobj, &pcf8591_attr_group);
  184. exit_kfree:
  185. kfree(data);
  186. exit:
  187. return err;
  188. }
  189. static int pcf8591_remove(struct i2c_client *client)
  190. {
  191. sysfs_remove_group(&client->dev.kobj, &pcf8591_attr_group_opt);
  192. sysfs_remove_group(&client->dev.kobj, &pcf8591_attr_group);
  193. kfree(i2c_get_clientdata(client));
  194. return 0;
  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. /* The first byte transmitted contains the conversion code of the
  204. previous read cycle. FLUSH IT! */
  205. i2c_smbus_read_byte(client);
  206. }
  207. static int pcf8591_read_channel(struct device *dev, int channel)
  208. {
  209. u8 value;
  210. struct i2c_client *client = to_i2c_client(dev);
  211. struct pcf8591_data *data = i2c_get_clientdata(client);
  212. mutex_lock(&data->update_lock);
  213. if ((data->control & PCF8591_CONTROL_AICH_MASK) != channel) {
  214. data->control = (data->control & ~PCF8591_CONTROL_AICH_MASK)
  215. | channel;
  216. i2c_smbus_write_byte(client, data->control);
  217. /* The first byte transmitted contains the conversion code of
  218. the previous read cycle. FLUSH IT! */
  219. i2c_smbus_read_byte(client);
  220. }
  221. value = i2c_smbus_read_byte(client);
  222. mutex_unlock(&data->update_lock);
  223. if ((channel == 2 && input_mode == 2) ||
  224. (channel != 3 && (input_mode == 1 || input_mode == 3)))
  225. return (10 * REG_TO_SIGNED(value));
  226. else
  227. return (10 * value);
  228. }
  229. static const struct i2c_device_id pcf8591_id[] = {
  230. { "pcf8591", 0 },
  231. { }
  232. };
  233. MODULE_DEVICE_TABLE(i2c, pcf8591_id);
  234. static struct i2c_driver pcf8591_driver = {
  235. .driver = {
  236. .name = "pcf8591",
  237. },
  238. .probe = pcf8591_probe,
  239. .remove = pcf8591_remove,
  240. .id_table = pcf8591_id,
  241. .class = I2C_CLASS_HWMON, /* Nearest choice */
  242. .detect = pcf8591_detect,
  243. .address_list = normal_i2c,
  244. };
  245. static int __init pcf8591_init(void)
  246. {
  247. if (input_mode < 0 || input_mode > 3) {
  248. printk(KERN_WARNING "pcf8591: invalid input_mode (%d)\n",
  249. 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);