pcf8591.c 9.1 KB

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