pcf8591.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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 i2c_client client;
  61. struct mutex update_lock;
  62. u8 control;
  63. u8 aout;
  64. };
  65. static int pcf8591_attach_adapter(struct i2c_adapter *adapter);
  66. static int pcf8591_detect(struct i2c_adapter *adapter, int address, int kind);
  67. static int pcf8591_detach_client(struct i2c_client *client);
  68. static void pcf8591_init_client(struct i2c_client *client);
  69. static int pcf8591_read_channel(struct device *dev, int channel);
  70. /* This is the driver that will be inserted */
  71. static struct i2c_driver pcf8591_driver = {
  72. .driver = {
  73. .name = "pcf8591",
  74. },
  75. .attach_adapter = pcf8591_attach_adapter,
  76. .detach_client = pcf8591_detach_client,
  77. };
  78. /* following are the sysfs callback functions */
  79. #define show_in_channel(channel) \
  80. static ssize_t show_in##channel##_input(struct device *dev, struct device_attribute *attr, char *buf) \
  81. { \
  82. return sprintf(buf, "%d\n", pcf8591_read_channel(dev, channel));\
  83. } \
  84. static DEVICE_ATTR(in##channel##_input, S_IRUGO, \
  85. show_in##channel##_input, NULL);
  86. show_in_channel(0);
  87. show_in_channel(1);
  88. show_in_channel(2);
  89. show_in_channel(3);
  90. static ssize_t show_out0_ouput(struct device *dev, struct device_attribute *attr, char *buf)
  91. {
  92. struct pcf8591_data *data = i2c_get_clientdata(to_i2c_client(dev));
  93. return sprintf(buf, "%d\n", data->aout * 10);
  94. }
  95. static ssize_t set_out0_output(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  96. {
  97. unsigned int value;
  98. struct i2c_client *client = to_i2c_client(dev);
  99. struct pcf8591_data *data = i2c_get_clientdata(client);
  100. if ((value = (simple_strtoul(buf, NULL, 10) + 5) / 10) <= 255) {
  101. data->aout = value;
  102. i2c_smbus_write_byte_data(client, data->control, data->aout);
  103. return count;
  104. }
  105. return -EINVAL;
  106. }
  107. static DEVICE_ATTR(out0_output, S_IWUSR | S_IRUGO,
  108. show_out0_ouput, set_out0_output);
  109. static ssize_t show_out0_enable(struct device *dev, struct device_attribute *attr, char *buf)
  110. {
  111. struct pcf8591_data *data = i2c_get_clientdata(to_i2c_client(dev));
  112. return sprintf(buf, "%u\n", !(!(data->control & PCF8591_CONTROL_AOEF)));
  113. }
  114. static ssize_t set_out0_enable(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  115. {
  116. struct i2c_client *client = to_i2c_client(dev);
  117. struct pcf8591_data *data = i2c_get_clientdata(client);
  118. unsigned long val = simple_strtoul(buf, NULL, 10);
  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(out0_enable, S_IWUSR | S_IRUGO,
  129. show_out0_enable, set_out0_enable);
  130. static struct attribute *pcf8591_attributes[] = {
  131. &dev_attr_out0_enable.attr,
  132. &dev_attr_out0_output.attr,
  133. &dev_attr_in0_input.attr,
  134. &dev_attr_in1_input.attr,
  135. NULL
  136. };
  137. static const struct attribute_group pcf8591_attr_group = {
  138. .attrs = pcf8591_attributes,
  139. };
  140. static struct attribute *pcf8591_attributes_opt[] = {
  141. &dev_attr_in2_input.attr,
  142. &dev_attr_in3_input.attr,
  143. NULL
  144. };
  145. static const struct attribute_group pcf8591_attr_group_opt = {
  146. .attrs = pcf8591_attributes_opt,
  147. };
  148. /*
  149. * Real code
  150. */
  151. static int pcf8591_attach_adapter(struct i2c_adapter *adapter)
  152. {
  153. return i2c_probe(adapter, &addr_data, pcf8591_detect);
  154. }
  155. /* This function is called by i2c_probe */
  156. static int pcf8591_detect(struct i2c_adapter *adapter, int address, int kind)
  157. {
  158. struct i2c_client *client;
  159. struct pcf8591_data *data;
  160. int err = 0;
  161. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE
  162. | I2C_FUNC_SMBUS_WRITE_BYTE_DATA))
  163. goto exit;
  164. /* OK. For now, we presume we have a valid client. We now create the
  165. client structure, even though we cannot fill it completely yet. */
  166. if (!(data = kzalloc(sizeof(struct pcf8591_data), GFP_KERNEL))) {
  167. err = -ENOMEM;
  168. goto exit;
  169. }
  170. client = &data->client;
  171. i2c_set_clientdata(client, data);
  172. client->addr = address;
  173. client->adapter = adapter;
  174. client->driver = &pcf8591_driver;
  175. /* Now, we would do the remaining detection. But the PCF8591 is plainly
  176. impossible to detect! Stupid chip. */
  177. /* Determine the chip type - only one kind supported! */
  178. if (kind <= 0)
  179. kind = pcf8591;
  180. /* Fill in the remaining client fields and put it into the global
  181. list */
  182. strlcpy(client->name, "pcf8591", I2C_NAME_SIZE);
  183. mutex_init(&data->update_lock);
  184. /* Tell the I2C layer a new client has arrived */
  185. if ((err = i2c_attach_client(client)))
  186. goto exit_kfree;
  187. /* Initialize the PCF8591 chip */
  188. pcf8591_init_client(client);
  189. /* Register sysfs hooks */
  190. err = sysfs_create_group(&client->dev.kobj, &pcf8591_attr_group);
  191. if (err)
  192. goto exit_detach;
  193. /* Register input2 if not in "two differential inputs" mode */
  194. if (input_mode != 3) {
  195. if ((err = device_create_file(&client->dev,
  196. &dev_attr_in2_input)))
  197. goto exit_sysfs_remove;
  198. }
  199. /* Register input3 only in "four single ended inputs" mode */
  200. if (input_mode == 0) {
  201. if ((err = device_create_file(&client->dev,
  202. &dev_attr_in3_input)))
  203. goto exit_sysfs_remove;
  204. }
  205. return 0;
  206. exit_sysfs_remove:
  207. sysfs_remove_group(&client->dev.kobj, &pcf8591_attr_group_opt);
  208. sysfs_remove_group(&client->dev.kobj, &pcf8591_attr_group);
  209. exit_detach:
  210. i2c_detach_client(client);
  211. exit_kfree:
  212. kfree(data);
  213. exit:
  214. return err;
  215. }
  216. static int pcf8591_detach_client(struct i2c_client *client)
  217. {
  218. int err;
  219. sysfs_remove_group(&client->dev.kobj, &pcf8591_attr_group_opt);
  220. sysfs_remove_group(&client->dev.kobj, &pcf8591_attr_group);
  221. if ((err = i2c_detach_client(client)))
  222. return err;
  223. kfree(i2c_get_clientdata(client));
  224. return 0;
  225. }
  226. /* Called when we have found a new PCF8591. */
  227. static void pcf8591_init_client(struct i2c_client *client)
  228. {
  229. struct pcf8591_data *data = i2c_get_clientdata(client);
  230. data->control = PCF8591_INIT_CONTROL;
  231. data->aout = PCF8591_INIT_AOUT;
  232. i2c_smbus_write_byte_data(client, data->control, data->aout);
  233. /* The first byte transmitted contains the conversion code of the
  234. previous read cycle. FLUSH IT! */
  235. i2c_smbus_read_byte(client);
  236. }
  237. static int pcf8591_read_channel(struct device *dev, int channel)
  238. {
  239. u8 value;
  240. struct i2c_client *client = to_i2c_client(dev);
  241. struct pcf8591_data *data = i2c_get_clientdata(client);
  242. mutex_lock(&data->update_lock);
  243. if ((data->control & PCF8591_CONTROL_AICH_MASK) != channel) {
  244. data->control = (data->control & ~PCF8591_CONTROL_AICH_MASK)
  245. | channel;
  246. i2c_smbus_write_byte(client, data->control);
  247. /* The first byte transmitted contains the conversion code of
  248. the previous read cycle. FLUSH IT! */
  249. i2c_smbus_read_byte(client);
  250. }
  251. value = i2c_smbus_read_byte(client);
  252. mutex_unlock(&data->update_lock);
  253. if ((channel == 2 && input_mode == 2) ||
  254. (channel != 3 && (input_mode == 1 || input_mode == 3)))
  255. return (10 * REG_TO_SIGNED(value));
  256. else
  257. return (10 * value);
  258. }
  259. static int __init pcf8591_init(void)
  260. {
  261. if (input_mode < 0 || input_mode > 3) {
  262. printk(KERN_WARNING "pcf8591: invalid input_mode (%d)\n",
  263. input_mode);
  264. input_mode = 0;
  265. }
  266. return i2c_add_driver(&pcf8591_driver);
  267. }
  268. static void __exit pcf8591_exit(void)
  269. {
  270. i2c_del_driver(&pcf8591_driver);
  271. }
  272. MODULE_AUTHOR("Aurelien Jarno <aurelien@aurel32.net>");
  273. MODULE_DESCRIPTION("PCF8591 driver");
  274. MODULE_LICENSE("GPL");
  275. module_init(pcf8591_init);
  276. module_exit(pcf8591_exit);