mcp3021.c 3.9 KB

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