pcf8591.c 9.7 KB

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