mcp3021.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 MCP3221_SAR_SHIFT 0
  33. #define MCP3221_SAR_MASK 0xfff
  34. #define MCP3221_OUTPUT_RES 12 /* 12-bit resolution */
  35. enum chips {
  36. mcp3021,
  37. mcp3221
  38. };
  39. /*
  40. * Client data (each client gets its own)
  41. */
  42. struct mcp3021_data {
  43. struct device *hwmon_dev;
  44. u32 vdd; /* device power supply */
  45. u16 sar_shift;
  46. u16 sar_mask;
  47. u8 output_res;
  48. };
  49. static int mcp3021_read16(struct i2c_client *client)
  50. {
  51. struct mcp3021_data *data = i2c_get_clientdata(client);
  52. int ret;
  53. u16 reg;
  54. __be16 buf;
  55. ret = i2c_master_recv(client, (char *)&buf, 2);
  56. if (ret < 0)
  57. return ret;
  58. if (ret != 2)
  59. return -EIO;
  60. /* The output code of the MCP3021 is transmitted with MSB first. */
  61. reg = be16_to_cpu(buf);
  62. /*
  63. * The ten-bit output code is composed of the lower 4-bit of the
  64. * first byte and the upper 6-bit of the second byte.
  65. */
  66. reg = (reg >> data->sar_shift) & data->sar_mask;
  67. return reg;
  68. }
  69. static inline u16 volts_from_reg(struct mcp3021_data *data, u16 val)
  70. {
  71. return DIV_ROUND_CLOSEST(data->vdd * val, 1 << data->output_res);
  72. }
  73. static ssize_t show_in_input(struct device *dev, struct device_attribute *attr,
  74. char *buf)
  75. {
  76. struct i2c_client *client = to_i2c_client(dev);
  77. struct mcp3021_data *data = i2c_get_clientdata(client);
  78. int reg, in_input;
  79. reg = mcp3021_read16(client);
  80. if (reg < 0)
  81. return reg;
  82. in_input = volts_from_reg(data, reg);
  83. return sprintf(buf, "%d\n", in_input);
  84. }
  85. static DEVICE_ATTR(in0_input, S_IRUGO, show_in_input, NULL);
  86. static int mcp3021_probe(struct i2c_client *client,
  87. const struct i2c_device_id *id)
  88. {
  89. int err;
  90. struct mcp3021_data *data = NULL;
  91. if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
  92. return -ENODEV;
  93. data = devm_kzalloc(&client->dev, sizeof(struct mcp3021_data),
  94. GFP_KERNEL);
  95. if (!data)
  96. return -ENOMEM;
  97. i2c_set_clientdata(client, data);
  98. switch (id->driver_data) {
  99. case mcp3021:
  100. data->sar_shift = MCP3021_SAR_SHIFT;
  101. data->sar_mask = MCP3021_SAR_MASK;
  102. data->output_res = MCP3021_OUTPUT_RES;
  103. break;
  104. case mcp3221:
  105. data->sar_shift = MCP3221_SAR_SHIFT;
  106. data->sar_mask = MCP3221_SAR_MASK;
  107. data->output_res = MCP3221_OUTPUT_RES;
  108. break;
  109. }
  110. if (dev_get_platdata(&client->dev)) {
  111. data->vdd = *(u32 *)dev_get_platdata(&client->dev);
  112. if (data->vdd > MCP3021_VDD_MAX || data->vdd < MCP3021_VDD_MIN)
  113. return -EINVAL;
  114. } else {
  115. data->vdd = MCP3021_VDD_REF;
  116. }
  117. err = sysfs_create_file(&client->dev.kobj, &dev_attr_in0_input.attr);
  118. if (err)
  119. return err;
  120. data->hwmon_dev = hwmon_device_register(&client->dev);
  121. if (IS_ERR(data->hwmon_dev)) {
  122. err = PTR_ERR(data->hwmon_dev);
  123. goto exit_remove;
  124. }
  125. return 0;
  126. exit_remove:
  127. sysfs_remove_file(&client->dev.kobj, &dev_attr_in0_input.attr);
  128. return err;
  129. }
  130. static int mcp3021_remove(struct i2c_client *client)
  131. {
  132. struct mcp3021_data *data = i2c_get_clientdata(client);
  133. hwmon_device_unregister(data->hwmon_dev);
  134. sysfs_remove_file(&client->dev.kobj, &dev_attr_in0_input.attr);
  135. return 0;
  136. }
  137. static const struct i2c_device_id mcp3021_id[] = {
  138. { "mcp3021", mcp3021 },
  139. { "mcp3221", mcp3221 },
  140. { }
  141. };
  142. MODULE_DEVICE_TABLE(i2c, mcp3021_id);
  143. static struct i2c_driver mcp3021_driver = {
  144. .driver = {
  145. .name = "mcp3021",
  146. },
  147. .probe = mcp3021_probe,
  148. .remove = mcp3021_remove,
  149. .id_table = mcp3021_id,
  150. };
  151. module_i2c_driver(mcp3021_driver);
  152. MODULE_AUTHOR("Mingkai Hu <Mingkai.hu@freescale.com>");
  153. MODULE_DESCRIPTION("Microchip MCP3021/MCP3221 driver");
  154. MODULE_LICENSE("GPL");