pcf8591.c 9.3 KB

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