pcf8591.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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 const 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. .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. mutex_lock(&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. mutex_unlock(&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. static struct attribute *pcf8591_attributes[] = {
  133. &dev_attr_out0_enable.attr,
  134. &dev_attr_out0_output.attr,
  135. &dev_attr_in0_input.attr,
  136. &dev_attr_in1_input.attr,
  137. NULL
  138. };
  139. static const struct attribute_group pcf8591_attr_group = {
  140. .attrs = pcf8591_attributes,
  141. };
  142. static struct attribute *pcf8591_attributes_opt[] = {
  143. &dev_attr_in2_input.attr,
  144. &dev_attr_in3_input.attr,
  145. NULL
  146. };
  147. static const struct attribute_group pcf8591_attr_group_opt = {
  148. .attrs = pcf8591_attributes_opt,
  149. };
  150. /*
  151. * Real code
  152. */
  153. static int pcf8591_attach_adapter(struct i2c_adapter *adapter)
  154. {
  155. return i2c_probe(adapter, &addr_data, pcf8591_detect);
  156. }
  157. /* This function is called by i2c_probe */
  158. static int pcf8591_detect(struct i2c_adapter *adapter, int address, int kind)
  159. {
  160. struct i2c_client *new_client;
  161. struct pcf8591_data *data;
  162. int err = 0;
  163. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_BYTE
  164. | I2C_FUNC_SMBUS_WRITE_BYTE_DATA))
  165. goto exit;
  166. /* OK. For now, we presume we have a valid client. We now create the
  167. client structure, even though we cannot fill it completely yet. */
  168. if (!(data = kzalloc(sizeof(struct pcf8591_data), GFP_KERNEL))) {
  169. err = -ENOMEM;
  170. goto exit;
  171. }
  172. new_client = &data->client;
  173. i2c_set_clientdata(new_client, data);
  174. new_client->addr = address;
  175. new_client->adapter = adapter;
  176. new_client->driver = &pcf8591_driver;
  177. new_client->flags = 0;
  178. /* Now, we would do the remaining detection. But the PCF8591 is plainly
  179. impossible to detect! Stupid chip. */
  180. /* Determine the chip type - only one kind supported! */
  181. if (kind <= 0)
  182. kind = pcf8591;
  183. /* Fill in the remaining client fields and put it into the global
  184. list */
  185. strlcpy(new_client->name, "pcf8591", I2C_NAME_SIZE);
  186. mutex_init(&data->update_lock);
  187. /* Tell the I2C layer a new client has arrived */
  188. if ((err = i2c_attach_client(new_client)))
  189. goto exit_kfree;
  190. /* Initialize the PCF8591 chip */
  191. pcf8591_init_client(new_client);
  192. /* Register sysfs hooks */
  193. err = sysfs_create_group(&new_client->dev.kobj, &pcf8591_attr_group);
  194. if (err)
  195. goto exit_detach;
  196. /* Register input2 if not in "two differential inputs" mode */
  197. if (input_mode != 3) {
  198. if ((err = device_create_file(&new_client->dev,
  199. &dev_attr_in2_input)))
  200. goto exit_sysfs_remove;
  201. }
  202. /* Register input3 only in "four single ended inputs" mode */
  203. if (input_mode == 0) {
  204. if ((err = device_create_file(&new_client->dev,
  205. &dev_attr_in3_input)))
  206. goto exit_sysfs_remove;
  207. }
  208. return 0;
  209. exit_sysfs_remove:
  210. sysfs_remove_group(&new_client->dev.kobj, &pcf8591_attr_group_opt);
  211. sysfs_remove_group(&new_client->dev.kobj, &pcf8591_attr_group);
  212. exit_detach:
  213. i2c_detach_client(new_client);
  214. exit_kfree:
  215. kfree(data);
  216. exit:
  217. return err;
  218. }
  219. static int pcf8591_detach_client(struct i2c_client *client)
  220. {
  221. int err;
  222. sysfs_remove_group(&client->dev.kobj, &pcf8591_attr_group_opt);
  223. sysfs_remove_group(&client->dev.kobj, &pcf8591_attr_group);
  224. if ((err = i2c_detach_client(client)))
  225. return err;
  226. kfree(i2c_get_clientdata(client));
  227. return 0;
  228. }
  229. /* Called when we have found a new PCF8591. */
  230. static void pcf8591_init_client(struct i2c_client *client)
  231. {
  232. struct pcf8591_data *data = i2c_get_clientdata(client);
  233. data->control = PCF8591_INIT_CONTROL;
  234. data->aout = PCF8591_INIT_AOUT;
  235. i2c_smbus_write_byte_data(client, data->control, data->aout);
  236. /* The first byte transmitted contains the conversion code of the
  237. previous read cycle. FLUSH IT! */
  238. i2c_smbus_read_byte(client);
  239. }
  240. static int pcf8591_read_channel(struct device *dev, int channel)
  241. {
  242. u8 value;
  243. struct i2c_client *client = to_i2c_client(dev);
  244. struct pcf8591_data *data = i2c_get_clientdata(client);
  245. mutex_lock(&data->update_lock);
  246. if ((data->control & PCF8591_CONTROL_AICH_MASK) != channel) {
  247. data->control = (data->control & ~PCF8591_CONTROL_AICH_MASK)
  248. | channel;
  249. i2c_smbus_write_byte(client, data->control);
  250. /* The first byte transmitted contains the conversion code of
  251. the previous read cycle. FLUSH IT! */
  252. i2c_smbus_read_byte(client);
  253. }
  254. value = i2c_smbus_read_byte(client);
  255. mutex_unlock(&data->update_lock);
  256. if ((channel == 2 && input_mode == 2) ||
  257. (channel != 3 && (input_mode == 1 || input_mode == 3)))
  258. return (10 * REG_TO_SIGNED(value));
  259. else
  260. return (10 * value);
  261. }
  262. static int __init pcf8591_init(void)
  263. {
  264. if (input_mode < 0 || input_mode > 3) {
  265. printk(KERN_WARNING "pcf8591: invalid input_mode (%d)\n",
  266. input_mode);
  267. input_mode = 0;
  268. }
  269. return i2c_add_driver(&pcf8591_driver);
  270. }
  271. static void __exit pcf8591_exit(void)
  272. {
  273. i2c_del_driver(&pcf8591_driver);
  274. }
  275. MODULE_AUTHOR("Aurelien Jarno <aurelien@aurel32.net>");
  276. MODULE_DESCRIPTION("PCF8591 driver");
  277. MODULE_LICENSE("GPL");
  278. module_init(pcf8591_init);
  279. module_exit(pcf8591_exit);