pcf8591.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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 <khali@linux-fr.org>
  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 = kzalloc(sizeof(struct pcf8591_data), GFP_KERNEL);
  170. if (!data) {
  171. err = -ENOMEM;
  172. goto exit;
  173. }
  174. i2c_set_clientdata(client, data);
  175. mutex_init(&data->update_lock);
  176. /* Initialize the PCF8591 chip */
  177. pcf8591_init_client(client);
  178. /* Register sysfs hooks */
  179. err = sysfs_create_group(&client->dev.kobj, &pcf8591_attr_group);
  180. if (err)
  181. goto exit_kfree;
  182. /* Register input2 if not in "two differential inputs" mode */
  183. if (input_mode != 3) {
  184. err = device_create_file(&client->dev, &dev_attr_in2_input);
  185. if (err)
  186. goto exit_sysfs_remove;
  187. }
  188. /* Register input3 only in "four single ended inputs" mode */
  189. if (input_mode == 0) {
  190. err = device_create_file(&client->dev, &dev_attr_in3_input);
  191. if (err)
  192. goto exit_sysfs_remove;
  193. }
  194. data->hwmon_dev = hwmon_device_register(&client->dev);
  195. if (IS_ERR(data->hwmon_dev)) {
  196. err = PTR_ERR(data->hwmon_dev);
  197. goto exit_sysfs_remove;
  198. }
  199. return 0;
  200. exit_sysfs_remove:
  201. sysfs_remove_group(&client->dev.kobj, &pcf8591_attr_group_opt);
  202. sysfs_remove_group(&client->dev.kobj, &pcf8591_attr_group);
  203. exit_kfree:
  204. kfree(data);
  205. exit:
  206. return err;
  207. }
  208. static int pcf8591_remove(struct i2c_client *client)
  209. {
  210. struct pcf8591_data *data = i2c_get_clientdata(client);
  211. hwmon_device_unregister(data->hwmon_dev);
  212. sysfs_remove_group(&client->dev.kobj, &pcf8591_attr_group_opt);
  213. sysfs_remove_group(&client->dev.kobj, &pcf8591_attr_group);
  214. kfree(i2c_get_clientdata(client));
  215. return 0;
  216. }
  217. /* Called when we have found a new PCF8591. */
  218. static void pcf8591_init_client(struct i2c_client *client)
  219. {
  220. struct pcf8591_data *data = i2c_get_clientdata(client);
  221. data->control = PCF8591_INIT_CONTROL;
  222. data->aout = PCF8591_INIT_AOUT;
  223. i2c_smbus_write_byte_data(client, data->control, data->aout);
  224. /*
  225. * The first byte transmitted contains the conversion code of the
  226. * previous read cycle. FLUSH IT!
  227. */
  228. i2c_smbus_read_byte(client);
  229. }
  230. static int pcf8591_read_channel(struct device *dev, int channel)
  231. {
  232. u8 value;
  233. struct i2c_client *client = to_i2c_client(dev);
  234. struct pcf8591_data *data = i2c_get_clientdata(client);
  235. mutex_lock(&data->update_lock);
  236. if ((data->control & PCF8591_CONTROL_AICH_MASK) != channel) {
  237. data->control = (data->control & ~PCF8591_CONTROL_AICH_MASK)
  238. | channel;
  239. i2c_smbus_write_byte(client, data->control);
  240. /*
  241. * The first byte transmitted contains the conversion code of
  242. * the previous read cycle. FLUSH IT!
  243. */
  244. i2c_smbus_read_byte(client);
  245. }
  246. value = i2c_smbus_read_byte(client);
  247. mutex_unlock(&data->update_lock);
  248. if ((channel == 2 && input_mode == 2) ||
  249. (channel != 3 && (input_mode == 1 || input_mode == 3)))
  250. return 10 * REG_TO_SIGNED(value);
  251. else
  252. return 10 * value;
  253. }
  254. static const struct i2c_device_id pcf8591_id[] = {
  255. { "pcf8591", 0 },
  256. { }
  257. };
  258. MODULE_DEVICE_TABLE(i2c, pcf8591_id);
  259. static struct i2c_driver pcf8591_driver = {
  260. .driver = {
  261. .name = "pcf8591",
  262. },
  263. .probe = pcf8591_probe,
  264. .remove = pcf8591_remove,
  265. .id_table = pcf8591_id,
  266. };
  267. static int __init pcf8591_init(void)
  268. {
  269. if (input_mode < 0 || input_mode > 3) {
  270. pr_warn("invalid input_mode (%d)\n", input_mode);
  271. input_mode = 0;
  272. }
  273. return i2c_add_driver(&pcf8591_driver);
  274. }
  275. static void __exit pcf8591_exit(void)
  276. {
  277. i2c_del_driver(&pcf8591_driver);
  278. }
  279. MODULE_AUTHOR("Aurelien Jarno <aurelien@aurel32.net>");
  280. MODULE_DESCRIPTION("PCF8591 driver");
  281. MODULE_LICENSE("GPL");
  282. module_init(pcf8591_init);
  283. module_exit(pcf8591_exit);