pcf8591.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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 out0_output_show(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 out0_output_store(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_RW(out0_output);
  114. static ssize_t out0_enable_show(struct device *dev,
  115. struct device_attribute *attr, char *buf)
  116. {
  117. struct pcf8591_data *data = i2c_get_clientdata(to_i2c_client(dev));
  118. return sprintf(buf, "%u\n", !(!(data->control & PCF8591_CONTROL_AOEF)));
  119. }
  120. static ssize_t out0_enable_store(struct device *dev,
  121. struct device_attribute *attr,
  122. const char *buf, size_t count)
  123. {
  124. struct i2c_client *client = to_i2c_client(dev);
  125. struct pcf8591_data *data = i2c_get_clientdata(client);
  126. unsigned long val;
  127. int err;
  128. err = kstrtoul(buf, 10, &val);
  129. if (err)
  130. return err;
  131. mutex_lock(&data->update_lock);
  132. if (val)
  133. data->control |= PCF8591_CONTROL_AOEF;
  134. else
  135. data->control &= ~PCF8591_CONTROL_AOEF;
  136. i2c_smbus_write_byte(client, data->control);
  137. mutex_unlock(&data->update_lock);
  138. return count;
  139. }
  140. static DEVICE_ATTR_RW(out0_enable);
  141. static struct attribute *pcf8591_attributes[] = {
  142. &dev_attr_out0_enable.attr,
  143. &dev_attr_out0_output.attr,
  144. &dev_attr_in0_input.attr,
  145. &dev_attr_in1_input.attr,
  146. NULL
  147. };
  148. static const struct attribute_group pcf8591_attr_group = {
  149. .attrs = pcf8591_attributes,
  150. };
  151. static struct attribute *pcf8591_attributes_opt[] = {
  152. &dev_attr_in2_input.attr,
  153. &dev_attr_in3_input.attr,
  154. NULL
  155. };
  156. static const struct attribute_group pcf8591_attr_group_opt = {
  157. .attrs = pcf8591_attributes_opt,
  158. };
  159. /*
  160. * Real code
  161. */
  162. static int pcf8591_probe(struct i2c_client *client,
  163. const struct i2c_device_id *id)
  164. {
  165. struct pcf8591_data *data;
  166. int err;
  167. data = devm_kzalloc(&client->dev, sizeof(struct pcf8591_data),
  168. GFP_KERNEL);
  169. if (!data)
  170. return -ENOMEM;
  171. i2c_set_clientdata(client, data);
  172. mutex_init(&data->update_lock);
  173. /* Initialize the PCF8591 chip */
  174. pcf8591_init_client(client);
  175. /* Register sysfs hooks */
  176. err = sysfs_create_group(&client->dev.kobj, &pcf8591_attr_group);
  177. if (err)
  178. return err;
  179. /* Register input2 if not in "two differential inputs" mode */
  180. if (input_mode != 3) {
  181. err = device_create_file(&client->dev, &dev_attr_in2_input);
  182. if (err)
  183. goto exit_sysfs_remove;
  184. }
  185. /* Register input3 only in "four single ended inputs" mode */
  186. if (input_mode == 0) {
  187. err = device_create_file(&client->dev, &dev_attr_in3_input);
  188. if (err)
  189. goto exit_sysfs_remove;
  190. }
  191. data->hwmon_dev = hwmon_device_register(&client->dev);
  192. if (IS_ERR(data->hwmon_dev)) {
  193. err = PTR_ERR(data->hwmon_dev);
  194. goto exit_sysfs_remove;
  195. }
  196. return 0;
  197. exit_sysfs_remove:
  198. sysfs_remove_group(&client->dev.kobj, &pcf8591_attr_group_opt);
  199. sysfs_remove_group(&client->dev.kobj, &pcf8591_attr_group);
  200. return err;
  201. }
  202. static int pcf8591_remove(struct i2c_client *client)
  203. {
  204. struct pcf8591_data *data = i2c_get_clientdata(client);
  205. hwmon_device_unregister(data->hwmon_dev);
  206. sysfs_remove_group(&client->dev.kobj, &pcf8591_attr_group_opt);
  207. sysfs_remove_group(&client->dev.kobj, &pcf8591_attr_group);
  208. return 0;
  209. }
  210. /* Called when we have found a new PCF8591. */
  211. static void pcf8591_init_client(struct i2c_client *client)
  212. {
  213. struct pcf8591_data *data = i2c_get_clientdata(client);
  214. data->control = PCF8591_INIT_CONTROL;
  215. data->aout = PCF8591_INIT_AOUT;
  216. i2c_smbus_write_byte_data(client, data->control, data->aout);
  217. /*
  218. * The first byte transmitted contains the conversion code of the
  219. * previous read cycle. FLUSH IT!
  220. */
  221. i2c_smbus_read_byte(client);
  222. }
  223. static int pcf8591_read_channel(struct device *dev, int channel)
  224. {
  225. u8 value;
  226. struct i2c_client *client = to_i2c_client(dev);
  227. struct pcf8591_data *data = i2c_get_clientdata(client);
  228. mutex_lock(&data->update_lock);
  229. if ((data->control & PCF8591_CONTROL_AICH_MASK) != channel) {
  230. data->control = (data->control & ~PCF8591_CONTROL_AICH_MASK)
  231. | channel;
  232. i2c_smbus_write_byte(client, data->control);
  233. /*
  234. * The first byte transmitted contains the conversion code of
  235. * the previous read cycle. FLUSH IT!
  236. */
  237. i2c_smbus_read_byte(client);
  238. }
  239. value = i2c_smbus_read_byte(client);
  240. mutex_unlock(&data->update_lock);
  241. if ((channel == 2 && input_mode == 2) ||
  242. (channel != 3 && (input_mode == 1 || input_mode == 3)))
  243. return 10 * REG_TO_SIGNED(value);
  244. else
  245. return 10 * value;
  246. }
  247. static const struct i2c_device_id pcf8591_id[] = {
  248. { "pcf8591", 0 },
  249. { }
  250. };
  251. MODULE_DEVICE_TABLE(i2c, pcf8591_id);
  252. static struct i2c_driver pcf8591_driver = {
  253. .driver = {
  254. .name = "pcf8591",
  255. },
  256. .probe = pcf8591_probe,
  257. .remove = pcf8591_remove,
  258. .id_table = pcf8591_id,
  259. };
  260. static int __init pcf8591_init(void)
  261. {
  262. if (input_mode < 0 || input_mode > 3) {
  263. pr_warn("invalid input_mode (%d)\n", input_mode);
  264. input_mode = 0;
  265. }
  266. return i2c_add_driver(&pcf8591_driver);
  267. }
  268. static void __exit pcf8591_exit(void)
  269. {
  270. i2c_del_driver(&pcf8591_driver);
  271. }
  272. MODULE_AUTHOR("Aurelien Jarno <aurelien@aurel32.net>");
  273. MODULE_DESCRIPTION("PCF8591 driver");
  274. MODULE_LICENSE("GPL");
  275. module_init(pcf8591_init);
  276. module_exit(pcf8591_exit);