pcf8591.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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. I2C_CLIENT_INSMOD_1(pcf8591);
  27. static int input_mode;
  28. module_param(input_mode, int, 0);
  29. MODULE_PARM_DESC(input_mode,
  30. "Analog input mode:\n"
  31. " 0 = four single ended inputs\n"
  32. " 1 = three differential inputs\n"
  33. " 2 = single ended and differential mixed\n"
  34. " 3 = two differential inputs\n");
  35. /* The PCF8591 control byte
  36. 7 6 5 4 3 2 1 0
  37. | 0 |AOEF| AIP | 0 |AINC| AICH | */
  38. /* Analog Output Enable Flag (analog output active if 1) */
  39. #define PCF8591_CONTROL_AOEF 0x40
  40. /* Analog Input Programming
  41. 0x00 = four single ended inputs
  42. 0x10 = three differential inputs
  43. 0x20 = single ended and differential mixed
  44. 0x30 = two differential inputs */
  45. #define PCF8591_CONTROL_AIP_MASK 0x30
  46. /* Autoincrement Flag (switch on if 1) */
  47. #define PCF8591_CONTROL_AINC 0x04
  48. /* Channel selection
  49. 0x00 = channel 0
  50. 0x01 = channel 1
  51. 0x02 = channel 2
  52. 0x03 = channel 3 */
  53. #define PCF8591_CONTROL_AICH_MASK 0x03
  54. /* Initial values */
  55. #define PCF8591_INIT_CONTROL ((input_mode << 4) | PCF8591_CONTROL_AOEF)
  56. #define PCF8591_INIT_AOUT 0 /* DAC out = 0 */
  57. /* Conversions */
  58. #define REG_TO_SIGNED(reg) (((reg) & 0x80)?((reg) - 256):(reg))
  59. struct pcf8591_data {
  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. /* Return 0 if detection is successful, -ENODEV otherwise */
  140. static int pcf8591_detect(struct i2c_client *client, int kind,
  141. struct i2c_board_info *info)
  142. {
  143. struct i2c_adapter *adapter = client->adapter;
  144. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE
  145. | I2C_FUNC_SMBUS_WRITE_BYTE_DATA))
  146. return -ENODEV;
  147. /* Now, we would do the remaining detection. But the PCF8591 is plainly
  148. impossible to detect! Stupid chip. */
  149. strlcpy(info->type, "pcf8591", I2C_NAME_SIZE);
  150. return 0;
  151. }
  152. static int pcf8591_probe(struct i2c_client *client,
  153. const struct i2c_device_id *id)
  154. {
  155. struct pcf8591_data *data;
  156. int err;
  157. if (!(data = kzalloc(sizeof(struct pcf8591_data), GFP_KERNEL))) {
  158. err = -ENOMEM;
  159. goto exit;
  160. }
  161. i2c_set_clientdata(client, data);
  162. mutex_init(&data->update_lock);
  163. /* Initialize the PCF8591 chip */
  164. pcf8591_init_client(client);
  165. /* Register sysfs hooks */
  166. err = sysfs_create_group(&client->dev.kobj, &pcf8591_attr_group);
  167. if (err)
  168. goto exit_kfree;
  169. /* Register input2 if not in "two differential inputs" mode */
  170. if (input_mode != 3) {
  171. if ((err = device_create_file(&client->dev,
  172. &dev_attr_in2_input)))
  173. goto exit_sysfs_remove;
  174. }
  175. /* Register input3 only in "four single ended inputs" mode */
  176. if (input_mode == 0) {
  177. if ((err = device_create_file(&client->dev,
  178. &dev_attr_in3_input)))
  179. goto exit_sysfs_remove;
  180. }
  181. return 0;
  182. exit_sysfs_remove:
  183. sysfs_remove_group(&client->dev.kobj, &pcf8591_attr_group_opt);
  184. sysfs_remove_group(&client->dev.kobj, &pcf8591_attr_group);
  185. exit_kfree:
  186. kfree(data);
  187. exit:
  188. return err;
  189. }
  190. static int pcf8591_remove(struct i2c_client *client)
  191. {
  192. sysfs_remove_group(&client->dev.kobj, &pcf8591_attr_group_opt);
  193. sysfs_remove_group(&client->dev.kobj, &pcf8591_attr_group);
  194. kfree(i2c_get_clientdata(client));
  195. return 0;
  196. }
  197. /* Called when we have found a new PCF8591. */
  198. static void pcf8591_init_client(struct i2c_client *client)
  199. {
  200. struct pcf8591_data *data = i2c_get_clientdata(client);
  201. data->control = PCF8591_INIT_CONTROL;
  202. data->aout = PCF8591_INIT_AOUT;
  203. i2c_smbus_write_byte_data(client, data->control, data->aout);
  204. /* The first byte transmitted contains the conversion code of the
  205. previous read cycle. FLUSH IT! */
  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. /* The first byte transmitted contains the conversion code of
  219. the previous read cycle. FLUSH IT! */
  220. i2c_smbus_read_byte(client);
  221. }
  222. value = i2c_smbus_read_byte(client);
  223. mutex_unlock(&data->update_lock);
  224. if ((channel == 2 && input_mode == 2) ||
  225. (channel != 3 && (input_mode == 1 || input_mode == 3)))
  226. return (10 * REG_TO_SIGNED(value));
  227. else
  228. return (10 * value);
  229. }
  230. static const struct i2c_device_id pcf8591_id[] = {
  231. { "pcf8591", 0 },
  232. { }
  233. };
  234. MODULE_DEVICE_TABLE(i2c, pcf8591_id);
  235. static struct i2c_driver pcf8591_driver = {
  236. .driver = {
  237. .name = "pcf8591",
  238. },
  239. .probe = pcf8591_probe,
  240. .remove = pcf8591_remove,
  241. .id_table = pcf8591_id,
  242. .class = I2C_CLASS_HWMON, /* Nearest choice */
  243. .detect = pcf8591_detect,
  244. .address_data = &addr_data,
  245. };
  246. static int __init pcf8591_init(void)
  247. {
  248. if (input_mode < 0 || input_mode > 3) {
  249. printk(KERN_WARNING "pcf8591: invalid input_mode (%d)\n",
  250. input_mode);
  251. input_mode = 0;
  252. }
  253. return i2c_add_driver(&pcf8591_driver);
  254. }
  255. static void __exit pcf8591_exit(void)
  256. {
  257. i2c_del_driver(&pcf8591_driver);
  258. }
  259. MODULE_AUTHOR("Aurelien Jarno <aurelien@aurel32.net>");
  260. MODULE_DESCRIPTION("PCF8591 driver");
  261. MODULE_LICENSE("GPL");
  262. module_init(pcf8591_init);
  263. module_exit(pcf8591_exit);