1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * mcp3021.c - driver for Microchip MCP3021 and MCP3221
4  *
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>
9  *
10  * This driver export 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.
13  */
14 
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>
22 #include <linux/of.h>
23 #include <linux/of_device.h>
24 
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
29 
30 /* output format */
31 #define MCP3021_SAR_SHIFT	2
32 #define MCP3021_SAR_MASK	0x3ff
33 #define MCP3021_OUTPUT_RES	10	/* 10-bit resolution */
34 
35 #define MCP3221_SAR_SHIFT	0
36 #define MCP3221_SAR_MASK	0xfff
37 #define MCP3221_OUTPUT_RES	12	/* 12-bit resolution */
38 
39 enum chips {
40 	mcp3021,
41 	mcp3221
42 };
43 
44 /*
45  * Client data (each client gets its own)
46  */
47 struct mcp3021_data {
48 	struct device *hwmon_dev;
49 	u32 vdd;        /* supply and reference voltage in millivolt */
50 	u16 sar_shift;
51 	u16 sar_mask;
52 	u8 output_res;
53 };
54 
mcp3021_read16(struct i2c_client * client)55 static int mcp3021_read16(struct i2c_client *client)
56 {
57 	struct mcp3021_data *data = i2c_get_clientdata(client);
58 	int ret;
59 	u16 reg;
60 	__be16 buf;
61 
62 	ret = i2c_master_recv(client, (char *)&buf, 2);
63 	if (ret < 0)
64 		return ret;
65 	if (ret != 2)
66 		return -EIO;
67 
68 	/* The output code of the MCP3021 is transmitted with MSB first. */
69 	reg = be16_to_cpu(buf);
70 
71 	/*
72 	 * The ten-bit output code is composed of the lower 4-bit of the
73 	 * first byte and the upper 6-bit of the second byte.
74 	 */
75 	reg = (reg >> data->sar_shift) & data->sar_mask;
76 
77 	return reg;
78 }
79 
volts_from_reg(struct mcp3021_data * data,u16 val)80 static inline u16 volts_from_reg(struct mcp3021_data *data, u16 val)
81 {
82 	return DIV_ROUND_CLOSEST(data->vdd * val, 1 << data->output_res);
83 }
84 
in0_input_show(struct device * dev,struct device_attribute * attr,char * buf)85 static ssize_t in0_input_show(struct device *dev,
86 			      struct device_attribute *attr, char *buf)
87 {
88 	struct i2c_client *client = to_i2c_client(dev);
89 	struct mcp3021_data *data = i2c_get_clientdata(client);
90 	int reg, in_input;
91 
92 	reg = mcp3021_read16(client);
93 	if (reg < 0)
94 		return reg;
95 
96 	in_input = volts_from_reg(data, reg);
97 
98 	return sprintf(buf, "%d\n", in_input);
99 }
100 
101 static DEVICE_ATTR_RO(in0_input);
102 
mcp3021_probe(struct i2c_client * client,const struct i2c_device_id * id)103 static int mcp3021_probe(struct i2c_client *client,
104 				const struct i2c_device_id *id)
105 {
106 	int err;
107 	struct mcp3021_data *data = NULL;
108 	struct device_node *np = client->dev.of_node;
109 
110 	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
111 		return -ENODEV;
112 
113 	data = devm_kzalloc(&client->dev, sizeof(struct mcp3021_data),
114 			    GFP_KERNEL);
115 	if (!data)
116 		return -ENOMEM;
117 
118 	i2c_set_clientdata(client, data);
119 
120 	if (np) {
121 		if (!of_property_read_u32(np, "reference-voltage-microvolt",
122 					  &data->vdd))
123 			data->vdd /= 1000;
124 		else
125 			data->vdd = MCP3021_VDD_REF_DEFAULT;
126 	} else {
127 		u32 *pdata = dev_get_platdata(&client->dev);
128 
129 		if (pdata)
130 			data->vdd = *pdata;
131 		else
132 			data->vdd = MCP3021_VDD_REF_DEFAULT;
133 	}
134 
135 	switch (id->driver_data) {
136 	case mcp3021:
137 		data->sar_shift = MCP3021_SAR_SHIFT;
138 		data->sar_mask = MCP3021_SAR_MASK;
139 		data->output_res = MCP3021_OUTPUT_RES;
140 		break;
141 
142 	case mcp3221:
143 		data->sar_shift = MCP3221_SAR_SHIFT;
144 		data->sar_mask = MCP3221_SAR_MASK;
145 		data->output_res = MCP3221_OUTPUT_RES;
146 		break;
147 	}
148 
149 	if (data->vdd > MCP3021_VDD_REF_MAX || data->vdd < MCP3021_VDD_REF_MIN)
150 		return -EINVAL;
151 
152 	err = sysfs_create_file(&client->dev.kobj, &dev_attr_in0_input.attr);
153 	if (err)
154 		return err;
155 
156 	data->hwmon_dev = hwmon_device_register(&client->dev);
157 	if (IS_ERR(data->hwmon_dev)) {
158 		err = PTR_ERR(data->hwmon_dev);
159 		goto exit_remove;
160 	}
161 
162 	return 0;
163 
164 exit_remove:
165 	sysfs_remove_file(&client->dev.kobj, &dev_attr_in0_input.attr);
166 	return err;
167 }
168 
mcp3021_remove(struct i2c_client * client)169 static int mcp3021_remove(struct i2c_client *client)
170 {
171 	struct mcp3021_data *data = i2c_get_clientdata(client);
172 
173 	hwmon_device_unregister(data->hwmon_dev);
174 	sysfs_remove_file(&client->dev.kobj, &dev_attr_in0_input.attr);
175 
176 	return 0;
177 }
178 
179 static const struct i2c_device_id mcp3021_id[] = {
180 	{ "mcp3021", mcp3021 },
181 	{ "mcp3221", mcp3221 },
182 	{ }
183 };
184 MODULE_DEVICE_TABLE(i2c, mcp3021_id);
185 
186 #ifdef CONFIG_OF
187 static const struct of_device_id of_mcp3021_match[] = {
188 	{ .compatible = "microchip,mcp3021", .data = (void *)mcp3021 },
189 	{ .compatible = "microchip,mcp3221", .data = (void *)mcp3221 },
190 	{ }
191 };
192 MODULE_DEVICE_TABLE(of, of_mcp3021_match);
193 #endif
194 
195 static struct i2c_driver mcp3021_driver = {
196 	.driver = {
197 		.name = "mcp3021",
198 		.of_match_table = of_match_ptr(of_mcp3021_match),
199 	},
200 	.probe = mcp3021_probe,
201 	.remove = mcp3021_remove,
202 	.id_table = mcp3021_id,
203 };
204 
205 module_i2c_driver(mcp3021_driver);
206 
207 MODULE_AUTHOR("Mingkai Hu <Mingkai.hu@freescale.com>");
208 MODULE_DESCRIPTION("Microchip MCP3021/MCP3221 driver");
209 MODULE_LICENSE("GPL");
210