mcp3021.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*
  2. * mcp3021.c - driver for Microchip MCP3021 and MCP3221
  3. *
  4. * Copyright (C) 2008-2009, 2012 Freescale Semiconductor, Inc.
  5. * Author: Mingkai Hu <Mingkai.hu@freescale.com>
  6. * Reworked by Sven Schuchmann <schuchmann@schleissheimer.de>
  7. *
  8. * This driver export the value of analog input voltage to sysfs, the
  9. * voltage unit is mV. Through the sysfs interface, lm-sensors tool
  10. * can also display the input voltage.
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2 of the License, or
  15. * (at your option) any later version.
  16. */
  17. #include <linux/kernel.h>
  18. #include <linux/module.h>
  19. #include <linux/hwmon.h>
  20. #include <linux/slab.h>
  21. #include <linux/i2c.h>
  22. #include <linux/err.h>
  23. #include <linux/device.h>
  24. /* Vdd info */
  25. #define MCP3021_VDD_MAX 5500
  26. #define MCP3021_VDD_MIN 2700
  27. #define MCP3021_VDD_REF 3300
  28. /* output format */
  29. #define MCP3021_SAR_SHIFT 2
  30. #define MCP3021_SAR_MASK 0x3ff
  31. #define MCP3021_OUTPUT_RES 10 /* 10-bit resolution */
  32. #define MCP3021_OUTPUT_SCALE 4
  33. #define MCP3221_SAR_SHIFT 0
  34. #define MCP3221_SAR_MASK 0xfff
  35. #define MCP3221_OUTPUT_RES 12 /* 12-bit resolution */
  36. #define MCP3221_OUTPUT_SCALE 1
  37. enum chips {
  38. mcp3021,
  39. mcp3221
  40. };
  41. /*
  42. * Client data (each client gets its own)
  43. */
  44. struct mcp3021_data {
  45. struct device *hwmon_dev;
  46. u32 vdd; /* device power supply */
  47. u16 sar_shift;
  48. u16 sar_mask;
  49. u8 output_res;
  50. u8 output_scale;
  51. };
  52. static int mcp3021_read16(struct i2c_client *client)
  53. {
  54. struct mcp3021_data *data = i2c_get_clientdata(client);
  55. int ret;
  56. u16 reg;
  57. __be16 buf;
  58. ret = i2c_master_recv(client, (char *)&buf, 2);
  59. if (ret < 0)
  60. return ret;
  61. if (ret != 2)
  62. return -EIO;
  63. /* The output code of the MCP3021 is transmitted with MSB first. */
  64. reg = be16_to_cpu(buf);
  65. /*
  66. * The ten-bit output code is composed of the lower 4-bit of the
  67. * first byte and the upper 6-bit of the second byte.
  68. */
  69. reg = (reg >> data->sar_shift) & data->sar_mask;
  70. return reg;
  71. }
  72. static inline u16 volts_from_reg(struct mcp3021_data *data, u16 val)
  73. {
  74. if (val == 0)
  75. return 0;
  76. val = val * data->output_scale - data->output_scale / 2;
  77. return val * DIV_ROUND_CLOSEST(data->vdd,
  78. (1 << data->output_res) * data->output_scale);
  79. }
  80. static ssize_t show_in_input(struct device *dev, struct device_attribute *attr,
  81. char *buf)
  82. {
  83. struct i2c_client *client = to_i2c_client(dev);
  84. struct mcp3021_data *data = i2c_get_clientdata(client);
  85. int reg, in_input;
  86. reg = mcp3021_read16(client);
  87. if (reg < 0)
  88. return reg;
  89. in_input = volts_from_reg(data, reg);
  90. return sprintf(buf, "%d\n", in_input);
  91. }
  92. static DEVICE_ATTR(in0_input, S_IRUGO, show_in_input, NULL);
  93. static int mcp3021_probe(struct i2c_client *client,
  94. const struct i2c_device_id *id)
  95. {
  96. int err;
  97. struct mcp3021_data *data = NULL;
  98. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
  99. return -ENODEV;
  100. data = devm_kzalloc(&client->dev, sizeof(struct mcp3021_data),
  101. GFP_KERNEL);
  102. if (!data)
  103. return -ENOMEM;
  104. i2c_set_clientdata(client, data);
  105. switch (id->driver_data) {
  106. case mcp3021:
  107. data->sar_shift = MCP3021_SAR_SHIFT;
  108. data->sar_mask = MCP3021_SAR_MASK;
  109. data->output_res = MCP3021_OUTPUT_RES;
  110. data->output_scale = MCP3021_OUTPUT_SCALE;
  111. break;
  112. case mcp3221:
  113. data->sar_shift = MCP3221_SAR_SHIFT;
  114. data->sar_mask = MCP3221_SAR_MASK;
  115. data->output_res = MCP3221_OUTPUT_RES;
  116. data->output_scale = MCP3221_OUTPUT_SCALE;
  117. break;
  118. }
  119. if (client->dev.platform_data) {
  120. data->vdd = *(u32 *)client->dev.platform_data;
  121. if (data->vdd > MCP3021_VDD_MAX || data->vdd < MCP3021_VDD_MIN)
  122. return -EINVAL;
  123. } else
  124. data->vdd = MCP3021_VDD_REF;
  125. err = sysfs_create_file(&client->dev.kobj, &dev_attr_in0_input.attr);
  126. if (err)
  127. return err;
  128. data->hwmon_dev = hwmon_device_register(&client->dev);
  129. if (IS_ERR(data->hwmon_dev)) {
  130. err = PTR_ERR(data->hwmon_dev);
  131. goto exit_remove;
  132. }
  133. return 0;
  134. exit_remove:
  135. sysfs_remove_file(&client->dev.kobj, &dev_attr_in0_input.attr);
  136. return err;
  137. }
  138. static int mcp3021_remove(struct i2c_client *client)
  139. {
  140. struct mcp3021_data *data = i2c_get_clientdata(client);
  141. hwmon_device_unregister(data->hwmon_dev);
  142. sysfs_remove_file(&client->dev.kobj, &dev_attr_in0_input.attr);
  143. return 0;
  144. }
  145. static const struct i2c_device_id mcp3021_id[] = {
  146. { "mcp3021", mcp3021 },
  147. { "mcp3221", mcp3221 },
  148. { }
  149. };
  150. MODULE_DEVICE_TABLE(i2c, mcp3021_id);
  151. static struct i2c_driver mcp3021_driver = {
  152. .driver = {
  153. .name = "mcp3021",
  154. },
  155. .probe = mcp3021_probe,
  156. .remove = mcp3021_remove,
  157. .id_table = mcp3021_id,
  158. };
  159. module_i2c_driver(mcp3021_driver);
  160. MODULE_AUTHOR("Mingkai Hu <Mingkai.hu@freescale.com>");
  161. MODULE_DESCRIPTION("Microchip MCP3021/MCP3221 driver");
  162. MODULE_LICENSE("GPL");