pcf8591.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. /*
  2. * Copyright (C) 2001-2004 Aurelien Jarno <aurelien@aurel32.net>
  3. * Ported to Linux 2.6 by Aurelien Jarno <aurelien@aurel32.net> with
  4. * the help of Jean Delvare <jdelvare@suse.de>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  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. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. */
  20. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  21. #include <linux/module.h>
  22. #include <linux/init.h>
  23. #include <linux/slab.h>
  24. #include <linux/i2c.h>
  25. #include <linux/mutex.h>
  26. #include <linux/err.h>
  27. #include <linux/hwmon.h>
  28. /* Insmod parameters */
  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. /*
  38. * The PCF8591 control byte
  39. * 7 6 5 4 3 2 1 0
  40. * | 0 |AOEF| AIP | 0 |AINC| AICH |
  41. */
  42. /* Analog Output Enable Flag (analog output active if 1) */
  43. #define PCF8591_CONTROL_AOEF 0x40
  44. /*
  45. * Analog Input Programming
  46. * 0x00 = four single ended inputs
  47. * 0x10 = three differential inputs
  48. * 0x20 = single ended and differential mixed
  49. * 0x30 = two differential inputs
  50. */
  51. #define PCF8591_CONTROL_AIP_MASK 0x30
  52. /* Autoincrement Flag (switch on if 1) */
  53. #define PCF8591_CONTROL_AINC 0x04
  54. /*
  55. * Channel selection
  56. * 0x00 = channel 0
  57. * 0x01 = channel 1
  58. * 0x02 = channel 2
  59. * 0x03 = channel 3
  60. */
  61. #define PCF8591_CONTROL_AICH_MASK 0x03
  62. /* Initial values */
  63. #define PCF8591_INIT_CONTROL ((input_mode << 4) | PCF8591_CONTROL_AOEF)
  64. #define PCF8591_INIT_AOUT 0 /* DAC out = 0 */
  65. /* Conversions */
  66. #define REG_TO_SIGNED(reg) (((reg) & 0x80) ? ((reg) - 256) : (reg))
  67. struct pcf8591_data {
  68. struct device *hwmon_dev;
  69. struct mutex update_lock;
  70. u8 control;
  71. u8 aout;
  72. };
  73. static void pcf8591_init_client(struct i2c_client *client);
  74. static int pcf8591_read_channel(struct device *dev, int channel);
  75. /* following are the sysfs callback functions */
  76. #define show_in_channel(channel) \
  77. static ssize_t show_in##channel##_input(struct device *dev, \
  78. struct device_attribute *attr, \
  79. char *buf) \
  80. { \
  81. return sprintf(buf, "%d\n", pcf8591_read_channel(dev, channel));\
  82. } \
  83. static DEVICE_ATTR(in##channel##_input, S_IRUGO, \
  84. show_in##channel##_input, NULL);
  85. show_in_channel(0);
  86. show_in_channel(1);
  87. show_in_channel(2);
  88. show_in_channel(3);
  89. static ssize_t show_out0_ouput(struct device *dev,
  90. struct device_attribute *attr, char *buf)
  91. {
  92. struct pcf8591_data *data = i2c_get_clientdata(to_i2c_client(dev));
  93. return sprintf(buf, "%d\n", data->aout * 10);
  94. }
  95. static ssize_t set_out0_output(struct device *dev,
  96. struct device_attribute *attr,
  97. const char *buf, size_t count)
  98. {
  99. unsigned long val;
  100. struct i2c_client *client = to_i2c_client(dev);
  101. struct pcf8591_data *data = i2c_get_clientdata(client);
  102. int err;
  103. err = kstrtoul(buf, 10, &val);
  104. if (err)
  105. return err;
  106. val /= 10;
  107. if (val > 255)
  108. return -EINVAL;
  109. data->aout = val;
  110. i2c_smbus_write_byte_data(client, data->control, data->aout);
  111. return count;
  112. }
  113. static DEVICE_ATTR(out0_output, S_IWUSR | S_IRUGO,
  114. show_out0_ouput, set_out0_output);
  115. static ssize_t show_out0_enable(struct device *dev,
  116. struct device_attribute *attr, char *buf)
  117. {
  118. struct pcf8591_data *data = i2c_get_clientdata(to_i2c_client(dev));
  119. return sprintf(buf, "%u\n", !(!(data->control & PCF8591_CONTROL_AOEF)));
  120. }
  121. static ssize_t set_out0_enable(struct device *dev,
  122. struct device_attribute *attr,
  123. const char *buf, size_t count)
  124. {
  125. struct i2c_client *client = to_i2c_client(dev);
  126. struct pcf8591_data *data = i2c_get_clientdata(client);
  127. unsigned long val;
  128. int err;
  129. err = kstrtoul(buf, 10, &val);
  130. if (err)
  131. return err;
  132. mutex_lock(&data->update_lock);
  133. if (val)
  134. data->control |= PCF8591_CONTROL_AOEF;
  135. else
  136. data->control &= ~PCF8591_CONTROL_AOEF;
  137. i2c_smbus_write_byte(client, data->control);
  138. mutex_unlock(&data->update_lock);
  139. return count;
  140. }
  141. static DEVICE_ATTR(out0_enable, S_IWUSR | S_IRUGO,
  142. show_out0_enable, set_out0_enable);
  143. static struct attribute *pcf8591_attributes[] = {
  144. &dev_attr_out0_enable.attr,
  145. &dev_attr_out0_output.attr,
  146. &dev_attr_in0_input.attr,
  147. &dev_attr_in1_input.attr,
  148. NULL
  149. };
  150. static const struct attribute_group pcf8591_attr_group = {
  151. .attrs = pcf8591_attributes,
  152. };
  153. static struct attribute *pcf8591_attributes_opt[] = {
  154. &dev_attr_in2_input.attr,
  155. &dev_attr_in3_input.attr,
  156. NULL
  157. };
  158. static const struct attribute_group pcf8591_attr_group_opt = {
  159. .attrs = pcf8591_attributes_opt,
  160. };
  161. /*
  162. * Real code
  163. */
  164. static int pcf8591_probe(struct i2c_client *client,
  165. const struct i2c_device_id *id)
  166. {
  167. struct pcf8591_data *data;
  168. int err;
  169. data = devm_kzalloc(&client->dev, sizeof(struct pcf8591_data),
  170. GFP_KERNEL);
  171. if (!data)
  172. return -ENOMEM;
  173. i2c_set_clientdata(client, data);
  174. mutex_init(&data->update_lock);
  175. /* Initialize the PCF8591 chip */
  176. pcf8591_init_client(client);
  177. /* Register sysfs hooks */
  178. err = sysfs_create_group(&client->dev.kobj, &pcf8591_attr_group);
  179. if (err)
  180. return err;
  181. /* Register input2 if not in "two differential inputs" mode */
  182. if (input_mode != 3) {
  183. err = device_create_file(&client->dev, &dev_attr_in2_input);
  184. if (err)
  185. goto exit_sysfs_remove;
  186. }
  187. /* Register input3 only in "four single ended inputs" mode */
  188. if (input_mode == 0) {
  189. err = device_create_file(&client->dev, &dev_attr_in3_input);
  190. if (err)
  191. goto exit_sysfs_remove;
  192. }
  193. data->hwmon_dev = hwmon_device_register(&client->dev);
  194. if (IS_ERR(data->hwmon_dev)) {
  195. err = PTR_ERR(data->hwmon_dev);
  196. goto exit_sysfs_remove;
  197. }
  198. return 0;
  199. exit_sysfs_remove:
  200. sysfs_remove_group(&client->dev.kobj, &pcf8591_attr_group_opt);
  201. sysfs_remove_group(&client->dev.kobj, &pcf8591_attr_group);
  202. return err;
  203. }
  204. static int pcf8591_remove(struct i2c_client *client)
  205. {
  206. struct pcf8591_data *data = i2c_get_clientdata(client);
  207. hwmon_device_unregister(data->hwmon_dev);
  208. sysfs_remove_group(&client->dev.kobj, &pcf8591_attr_group_opt);
  209. sysfs_remove_group(&client->dev.kobj, &pcf8591_attr_group);
  210. return 0;
  211. }
  212. /* Called when we have found a new PCF8591. */
  213. static void pcf8591_init_client(struct i2c_client *client)
  214. {
  215. struct pcf8591_data *data = i2c_get_clientdata(client);
  216. data->control = PCF8591_INIT_CONTROL;
  217. data->aout = PCF8591_INIT_AOUT;
  218. i2c_smbus_write_byte_data(client, data->control, data->aout);
  219. /*
  220. * The first byte transmitted contains the conversion code of the
  221. * previous read cycle. FLUSH IT!
  222. */
  223. i2c_smbus_read_byte(client);
  224. }
  225. static int pcf8591_read_channel(struct device *dev, int channel)
  226. {
  227. u8 value;
  228. struct i2c_client *client = to_i2c_client(dev);
  229. struct pcf8591_data *data = i2c_get_clientdata(client);
  230. mutex_lock(&data->update_lock);
  231. if ((data->control & PCF8591_CONTROL_AICH_MASK) != channel) {
  232. data->control = (data->control & ~PCF8591_CONTROL_AICH_MASK)
  233. | channel;
  234. i2c_smbus_write_byte(client, data->control);
  235. /*
  236. * The first byte transmitted contains the conversion code of
  237. * the previous read cycle. FLUSH IT!
  238. */
  239. i2c_smbus_read_byte(client);
  240. }
  241. value = i2c_smbus_read_byte(client);
  242. mutex_unlock(&data->update_lock);
  243. if ((channel == 2 && input_mode == 2) ||
  244. (channel != 3 && (input_mode == 1 || input_mode == 3)))
  245. return 10 * REG_TO_SIGNED(value);
  246. else
  247. return 10 * value;
  248. }
  249. static const struct i2c_device_id pcf8591_id[] = {
  250. { "pcf8591", 0 },
  251. { }
  252. };
  253. MODULE_DEVICE_TABLE(i2c, pcf8591_id);
  254. static struct i2c_driver pcf8591_driver = {
  255. .driver = {
  256. .name = "pcf8591",
  257. },
  258. .probe = pcf8591_probe,
  259. .remove = pcf8591_remove,
  260. .id_table = pcf8591_id,
  261. };
  262. static int __init pcf8591_init(void)
  263. {
  264. if (input_mode < 0 || input_mode > 3) {
  265. pr_warn("invalid input_mode (%d)\n", input_mode);
  266. input_mode = 0;
  267. }
  268. return i2c_add_driver(&pcf8591_driver);
  269. }
  270. static void __exit pcf8591_exit(void)
  271. {
  272. i2c_del_driver(&pcf8591_driver);
  273. }
  274. MODULE_AUTHOR("Aurelien Jarno <aurelien@aurel32.net>");
  275. MODULE_DESCRIPTION("PCF8591 driver");
  276. MODULE_LICENSE("GPL");
  277. module_init(pcf8591_init);
  278. module_exit(pcf8591_exit);