pcf8591.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /*
  2. pcf8591.c - Part of lm_sensors, Linux kernel modules for hardware
  3. monitoring
  4. Copyright (C) 2001-2004 Aurelien Jarno <aurelien@aurel32.net>
  5. Ported to Linux 2.6 by Aurelien Jarno <aurelien@aurel32.net> with
  6. the help of Jean Delvare <khali@linux-fr.org>
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. */
  19. #include <linux/module.h>
  20. #include <linux/init.h>
  21. #include <linux/slab.h>
  22. #include <linux/i2c.h>
  23. /* Addresses to scan */
  24. static unsigned short normal_i2c[] = { 0x48, 0x49, 0x4a, 0x4b, 0x4c,
  25. 0x4d, 0x4e, 0x4f, I2C_CLIENT_END };
  26. /* Insmod parameters */
  27. I2C_CLIENT_INSMOD_1(pcf8591);
  28. static int input_mode;
  29. module_param(input_mode, int, 0);
  30. MODULE_PARM_DESC(input_mode,
  31. "Analog input mode:\n"
  32. " 0 = four single ended inputs\n"
  33. " 1 = three differential inputs\n"
  34. " 2 = single ended and differential mixed\n"
  35. " 3 = two differential inputs\n");
  36. /* The PCF8591 control byte
  37. 7 6 5 4 3 2 1 0
  38. | 0 |AOEF| AIP | 0 |AINC| AICH | */
  39. /* Analog Output Enable Flag (analog output active if 1) */
  40. #define PCF8591_CONTROL_AOEF 0x40
  41. /* Analog Input Programming
  42. 0x00 = four single ended inputs
  43. 0x10 = three differential inputs
  44. 0x20 = single ended and differential mixed
  45. 0x30 = two differential inputs */
  46. #define PCF8591_CONTROL_AIP_MASK 0x30
  47. /* Autoincrement Flag (switch on if 1) */
  48. #define PCF8591_CONTROL_AINC 0x04
  49. /* Channel selection
  50. 0x00 = channel 0
  51. 0x01 = channel 1
  52. 0x02 = channel 2
  53. 0x03 = channel 3 */
  54. #define PCF8591_CONTROL_AICH_MASK 0x03
  55. /* Initial values */
  56. #define PCF8591_INIT_CONTROL ((input_mode << 4) | PCF8591_CONTROL_AOEF)
  57. #define PCF8591_INIT_AOUT 0 /* DAC out = 0 */
  58. /* Conversions */
  59. #define REG_TO_SIGNED(reg) (((reg) & 0x80)?((reg) - 256):(reg))
  60. struct pcf8591_data {
  61. struct i2c_client client;
  62. struct semaphore update_lock;
  63. u8 control;
  64. u8 aout;
  65. };
  66. static int pcf8591_attach_adapter(struct i2c_adapter *adapter);
  67. static int pcf8591_detect(struct i2c_adapter *adapter, int address, int kind);
  68. static int pcf8591_detach_client(struct i2c_client *client);
  69. static void pcf8591_init_client(struct i2c_client *client);
  70. static int pcf8591_read_channel(struct device *dev, int channel);
  71. /* This is the driver that will be inserted */
  72. static struct i2c_driver pcf8591_driver = {
  73. .driver = {
  74. .name = "pcf8591",
  75. },
  76. .id = I2C_DRIVERID_PCF8591,
  77. .attach_adapter = pcf8591_attach_adapter,
  78. .detach_client = pcf8591_detach_client,
  79. };
  80. /* following are the sysfs callback functions */
  81. #define show_in_channel(channel) \
  82. static ssize_t show_in##channel##_input(struct device *dev, struct device_attribute *attr, char *buf) \
  83. { \
  84. return sprintf(buf, "%d\n", pcf8591_read_channel(dev, channel));\
  85. } \
  86. static DEVICE_ATTR(in##channel##_input, S_IRUGO, \
  87. show_in##channel##_input, NULL);
  88. show_in_channel(0);
  89. show_in_channel(1);
  90. show_in_channel(2);
  91. show_in_channel(3);
  92. static ssize_t show_out0_ouput(struct device *dev, struct device_attribute *attr, char *buf)
  93. {
  94. struct pcf8591_data *data = i2c_get_clientdata(to_i2c_client(dev));
  95. return sprintf(buf, "%d\n", data->aout * 10);
  96. }
  97. static ssize_t set_out0_output(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  98. {
  99. unsigned int value;
  100. struct i2c_client *client = to_i2c_client(dev);
  101. struct pcf8591_data *data = i2c_get_clientdata(client);
  102. if ((value = (simple_strtoul(buf, NULL, 10) + 5) / 10) <= 255) {
  103. data->aout = value;
  104. i2c_smbus_write_byte_data(client, data->control, data->aout);
  105. return count;
  106. }
  107. return -EINVAL;
  108. }
  109. static DEVICE_ATTR(out0_output, S_IWUSR | S_IRUGO,
  110. show_out0_ouput, set_out0_output);
  111. static ssize_t show_out0_enable(struct device *dev, struct device_attribute *attr, char *buf)
  112. {
  113. struct pcf8591_data *data = i2c_get_clientdata(to_i2c_client(dev));
  114. return sprintf(buf, "%u\n", !(!(data->control & PCF8591_CONTROL_AOEF)));
  115. }
  116. static ssize_t set_out0_enable(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  117. {
  118. struct i2c_client *client = to_i2c_client(dev);
  119. struct pcf8591_data *data = i2c_get_clientdata(client);
  120. unsigned long val = simple_strtoul(buf, NULL, 10);
  121. down(&data->update_lock);
  122. if (val)
  123. data->control |= PCF8591_CONTROL_AOEF;
  124. else
  125. data->control &= ~PCF8591_CONTROL_AOEF;
  126. i2c_smbus_write_byte(client, data->control);
  127. up(&data->update_lock);
  128. return count;
  129. }
  130. static DEVICE_ATTR(out0_enable, S_IWUSR | S_IRUGO,
  131. show_out0_enable, set_out0_enable);
  132. /*
  133. * Real code
  134. */
  135. static int pcf8591_attach_adapter(struct i2c_adapter *adapter)
  136. {
  137. return i2c_probe(adapter, &addr_data, pcf8591_detect);
  138. }
  139. /* This function is called by i2c_probe */
  140. static int pcf8591_detect(struct i2c_adapter *adapter, int address, int kind)
  141. {
  142. struct i2c_client *new_client;
  143. struct pcf8591_data *data;
  144. int err = 0;
  145. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE
  146. | I2C_FUNC_SMBUS_WRITE_BYTE_DATA))
  147. goto exit;
  148. /* OK. For now, we presume we have a valid client. We now create the
  149. client structure, even though we cannot fill it completely yet. */
  150. if (!(data = kzalloc(sizeof(struct pcf8591_data), GFP_KERNEL))) {
  151. err = -ENOMEM;
  152. goto exit;
  153. }
  154. new_client = &data->client;
  155. i2c_set_clientdata(new_client, data);
  156. new_client->addr = address;
  157. new_client->adapter = adapter;
  158. new_client->driver = &pcf8591_driver;
  159. new_client->flags = 0;
  160. /* Now, we would do the remaining detection. But the PCF8591 is plainly
  161. impossible to detect! Stupid chip. */
  162. /* Determine the chip type - only one kind supported! */
  163. if (kind <= 0)
  164. kind = pcf8591;
  165. /* Fill in the remaining client fields and put it into the global
  166. list */
  167. strlcpy(new_client->name, "pcf8591", I2C_NAME_SIZE);
  168. init_MUTEX(&data->update_lock);
  169. /* Tell the I2C layer a new client has arrived */
  170. if ((err = i2c_attach_client(new_client)))
  171. goto exit_kfree;
  172. /* Initialize the PCF8591 chip */
  173. pcf8591_init_client(new_client);
  174. /* Register sysfs hooks */
  175. device_create_file(&new_client->dev, &dev_attr_out0_enable);
  176. device_create_file(&new_client->dev, &dev_attr_out0_output);
  177. device_create_file(&new_client->dev, &dev_attr_in0_input);
  178. device_create_file(&new_client->dev, &dev_attr_in1_input);
  179. /* Register input2 if not in "two differential inputs" mode */
  180. if (input_mode != 3 )
  181. device_create_file(&new_client->dev, &dev_attr_in2_input);
  182. /* Register input3 only in "four single ended inputs" mode */
  183. if (input_mode == 0)
  184. device_create_file(&new_client->dev, &dev_attr_in3_input);
  185. return 0;
  186. /* OK, this is not exactly good programming practice, usually. But it is
  187. very code-efficient in this case. */
  188. exit_kfree:
  189. kfree(data);
  190. exit:
  191. return err;
  192. }
  193. static int pcf8591_detach_client(struct i2c_client *client)
  194. {
  195. int err;
  196. if ((err = i2c_detach_client(client)))
  197. return err;
  198. kfree(i2c_get_clientdata(client));
  199. return 0;
  200. }
  201. /* Called when we have found a new PCF8591. */
  202. static void pcf8591_init_client(struct i2c_client *client)
  203. {
  204. struct pcf8591_data *data = i2c_get_clientdata(client);
  205. data->control = PCF8591_INIT_CONTROL;
  206. data->aout = PCF8591_INIT_AOUT;
  207. i2c_smbus_write_byte_data(client, data->control, data->aout);
  208. /* The first byte transmitted contains the conversion code of the
  209. previous read cycle. FLUSH IT! */
  210. i2c_smbus_read_byte(client);
  211. }
  212. static int pcf8591_read_channel(struct device *dev, int channel)
  213. {
  214. u8 value;
  215. struct i2c_client *client = to_i2c_client(dev);
  216. struct pcf8591_data *data = i2c_get_clientdata(client);
  217. down(&data->update_lock);
  218. if ((data->control & PCF8591_CONTROL_AICH_MASK) != channel) {
  219. data->control = (data->control & ~PCF8591_CONTROL_AICH_MASK)
  220. | channel;
  221. i2c_smbus_write_byte(client, data->control);
  222. /* The first byte transmitted contains the conversion code of
  223. the previous read cycle. FLUSH IT! */
  224. i2c_smbus_read_byte(client);
  225. }
  226. value = i2c_smbus_read_byte(client);
  227. up(&data->update_lock);
  228. if ((channel == 2 && input_mode == 2) ||
  229. (channel != 3 && (input_mode == 1 || input_mode == 3)))
  230. return (10 * REG_TO_SIGNED(value));
  231. else
  232. return (10 * value);
  233. }
  234. static int __init pcf8591_init(void)
  235. {
  236. if (input_mode < 0 || input_mode > 3) {
  237. printk(KERN_WARNING "pcf8591: invalid input_mode (%d)\n",
  238. input_mode);
  239. input_mode = 0;
  240. }
  241. return i2c_add_driver(&pcf8591_driver);
  242. }
  243. static void __exit pcf8591_exit(void)
  244. {
  245. i2c_del_driver(&pcf8591_driver);
  246. }
  247. MODULE_AUTHOR("Aurelien Jarno <aurelien@aurel32.net>");
  248. MODULE_DESCRIPTION("PCF8591 driver");
  249. MODULE_LICENSE("GPL");
  250. module_init(pcf8591_init);
  251. module_exit(pcf8591_exit);