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