1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * mcp3021.c - driver for Microchip MCP3021 and MCP3221
5 * Copyright (C) 2008-2009, 2012 Freescale Semiconductor, Inc.
6 * Author: Mingkai Hu <Mingkai.hu@freescale.com>
7 * Reworked by Sven Schuchmann <schuchmann@schleissheimer.de>
8 * DT support added by Clemens Gruber <clemens.gruber@pqgruber.com>
10 * This driver exports the value of analog input voltage to sysfs, the
11 * voltage unit is mV. Through the sysfs interface, lm-sensors tool
12 * can also display the input voltage.
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/hwmon.h>
18 #include <linux/slab.h>
19 #include <linux/i2c.h>
20 #include <linux/err.h>
21 #include <linux/device.h>
23 #include <linux/of_device.h>
25 /* Vdd / reference voltage in millivolt */
26 #define MCP3021_VDD_REF_MAX 5500
27 #define MCP3021_VDD_REF_MIN 2700
28 #define MCP3021_VDD_REF_DEFAULT 3300
31 #define MCP3021_SAR_SHIFT 2
32 #define MCP3021_SAR_MASK 0x3ff
33 #define MCP3021_OUTPUT_RES 10 /* 10-bit resolution */
35 #define MCP3221_SAR_SHIFT 0
36 #define MCP3221_SAR_MASK 0xfff
37 #define MCP3221_OUTPUT_RES 12 /* 12-bit resolution */
45 * Client data (each client gets its own)
48 struct i2c_client *client;
49 u32 vdd; /* supply and reference voltage in millivolt */
55 static inline u16 volts_from_reg(struct mcp3021_data *data, u16 val)
57 return DIV_ROUND_CLOSEST(data->vdd * val, 1 << data->output_res);
60 static int mcp3021_read(struct device *dev, enum hwmon_sensor_types type,
61 u32 attr, int channel, long *val)
63 struct mcp3021_data *data = dev_get_drvdata(dev);
64 struct i2c_client *client = data->client;
72 ret = i2c_master_recv(client, (char *)&buf, 2);
78 /* The output code of the MCP3021 is transmitted with MSB first. */
79 reg = be16_to_cpu(buf);
82 * The ten-bit output code is composed of the lower 4-bit of the
83 * first byte and the upper 6-bit of the second byte.
85 reg = (reg >> data->sar_shift) & data->sar_mask;
87 *val = volts_from_reg(data, reg);
92 static umode_t mcp3021_is_visible(const void *_data,
93 enum hwmon_sensor_types type,
94 u32 attr, int channel)
99 if (attr != hwmon_in_input)
105 static const struct hwmon_channel_info * const mcp3021_info[] = {
106 HWMON_CHANNEL_INFO(in, HWMON_I_INPUT),
110 static const struct hwmon_ops mcp3021_hwmon_ops = {
111 .is_visible = mcp3021_is_visible,
112 .read = mcp3021_read,
115 static const struct hwmon_chip_info mcp3021_chip_info = {
116 .ops = &mcp3021_hwmon_ops,
117 .info = mcp3021_info,
120 static const struct i2c_device_id mcp3021_id[];
122 static int mcp3021_probe(struct i2c_client *client)
124 struct mcp3021_data *data = NULL;
125 struct device_node *np = client->dev.of_node;
126 struct device *hwmon_dev;
128 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
131 data = devm_kzalloc(&client->dev, sizeof(struct mcp3021_data),
136 i2c_set_clientdata(client, data);
139 if (!of_property_read_u32(np, "reference-voltage-microvolt",
143 data->vdd = MCP3021_VDD_REF_DEFAULT;
145 u32 *pdata = dev_get_platdata(&client->dev);
150 data->vdd = MCP3021_VDD_REF_DEFAULT;
153 switch (i2c_match_id(mcp3021_id, client)->driver_data) {
155 data->sar_shift = MCP3021_SAR_SHIFT;
156 data->sar_mask = MCP3021_SAR_MASK;
157 data->output_res = MCP3021_OUTPUT_RES;
161 data->sar_shift = MCP3221_SAR_SHIFT;
162 data->sar_mask = MCP3221_SAR_MASK;
163 data->output_res = MCP3221_OUTPUT_RES;
167 data->client = client;
169 if (data->vdd > MCP3021_VDD_REF_MAX || data->vdd < MCP3021_VDD_REF_MIN)
172 hwmon_dev = devm_hwmon_device_register_with_info(&client->dev,
177 return PTR_ERR_OR_ZERO(hwmon_dev);
180 static const struct i2c_device_id mcp3021_id[] = {
181 { "mcp3021", mcp3021 },
182 { "mcp3221", mcp3221 },
185 MODULE_DEVICE_TABLE(i2c, mcp3021_id);
188 static const struct of_device_id of_mcp3021_match[] = {
189 { .compatible = "microchip,mcp3021", .data = (void *)mcp3021 },
190 { .compatible = "microchip,mcp3221", .data = (void *)mcp3221 },
193 MODULE_DEVICE_TABLE(of, of_mcp3021_match);
196 static struct i2c_driver mcp3021_driver = {
199 .of_match_table = of_match_ptr(of_mcp3021_match),
201 .probe = mcp3021_probe,
202 .id_table = mcp3021_id,
205 module_i2c_driver(mcp3021_driver);
207 MODULE_AUTHOR("Mingkai Hu <Mingkai.hu@freescale.com>");
208 MODULE_DESCRIPTION("Microchip MCP3021/MCP3221 driver");
209 MODULE_LICENSE("GPL");