blob: ce2780768074ca5cc6e8530c21c9031dac947df4 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Xie Xiaobo5510e622012-03-23 10:02:20 +01002/*
Sven Schuchmann592758b2012-09-21 13:04:22 +02003 * mcp3021.c - driver for Microchip MCP3021 and MCP3221
Xie Xiaobo5510e622012-03-23 10:02:20 +01004 *
5 * Copyright (C) 2008-2009, 2012 Freescale Semiconductor, Inc.
6 * Author: Mingkai Hu <Mingkai.hu@freescale.com>
Sven Schuchmann8b662f32012-09-21 13:04:21 +02007 * Reworked by Sven Schuchmann <schuchmann@schleissheimer.de>
Clemens Gruberf4dc8112016-11-09 22:22:34 +01008 * DT support added by Clemens Gruber <clemens.gruber@pqgruber.com>
Xie Xiaobo5510e622012-03-23 10:02:20 +01009 *
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.
Xie Xiaobo5510e622012-03-23 10:02:20 +010013 */
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>
Clemens Gruberf4dc8112016-11-09 22:22:34 +010022#include <linux/of.h>
23#include <linux/of_device.h>
Xie Xiaobo5510e622012-03-23 10:02:20 +010024
Clemens Gruberf4dc8112016-11-09 22:22:34 +010025/* 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
Xie Xiaobo5510e622012-03-23 10:02:20 +010029
30/* output format */
31#define MCP3021_SAR_SHIFT 2
32#define MCP3021_SAR_MASK 0x3ff
Xie Xiaobo5510e622012-03-23 10:02:20 +010033#define MCP3021_OUTPUT_RES 10 /* 10-bit resolution */
Xie Xiaobo5510e622012-03-23 10:02:20 +010034
Sven Schuchmann592758b2012-09-21 13:04:22 +020035#define MCP3221_SAR_SHIFT 0
36#define MCP3221_SAR_MASK 0xfff
37#define MCP3221_OUTPUT_RES 12 /* 12-bit resolution */
Sven Schuchmann592758b2012-09-21 13:04:22 +020038
Sven Schuchmann8b662f32012-09-21 13:04:21 +020039enum chips {
Sven Schuchmann592758b2012-09-21 13:04:22 +020040 mcp3021,
41 mcp3221
Sven Schuchmann8b662f32012-09-21 13:04:21 +020042};
Sven Schuchmann592758b2012-09-21 13:04:22 +020043
Xie Xiaobo5510e622012-03-23 10:02:20 +010044/*
45 * Client data (each client gets its own)
46 */
47struct mcp3021_data {
48 struct device *hwmon_dev;
Clemens Gruberf4dc8112016-11-09 22:22:34 +010049 u32 vdd; /* supply and reference voltage in millivolt */
Sven Schuchmann8b662f32012-09-21 13:04:21 +020050 u16 sar_shift;
51 u16 sar_mask;
52 u8 output_res;
Xie Xiaobo5510e622012-03-23 10:02:20 +010053};
54
55static int mcp3021_read16(struct i2c_client *client)
56{
Sven Schuchmann8b662f32012-09-21 13:04:21 +020057 struct mcp3021_data *data = i2c_get_clientdata(client);
Xie Xiaobo5510e622012-03-23 10:02:20 +010058 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 */
Sven Schuchmann8b662f32012-09-21 13:04:21 +020075 reg = (reg >> data->sar_shift) & data->sar_mask;
Xie Xiaobo5510e622012-03-23 10:02:20 +010076
77 return reg;
78}
79
Sven Schuchmann8b662f32012-09-21 13:04:21 +020080static inline u16 volts_from_reg(struct mcp3021_data *data, u16 val)
Xie Xiaobo5510e622012-03-23 10:02:20 +010081{
Stevens, Nick347d7e42015-07-01 16:07:41 +000082 return DIV_ROUND_CLOSEST(data->vdd * val, 1 << data->output_res);
Xie Xiaobo5510e622012-03-23 10:02:20 +010083}
84
Julia Lawall5c2f0dd2016-12-22 13:05:00 +010085static ssize_t in0_input_show(struct device *dev,
86 struct device_attribute *attr, char *buf)
Xie Xiaobo5510e622012-03-23 10:02:20 +010087{
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
Sven Schuchmann8b662f32012-09-21 13:04:21 +020096 in_input = volts_from_reg(data, reg);
97
Xie Xiaobo5510e622012-03-23 10:02:20 +010098 return sprintf(buf, "%d\n", in_input);
99}
100
Julia Lawall5c2f0dd2016-12-22 13:05:00 +0100101static DEVICE_ATTR_RO(in0_input);
Xie Xiaobo5510e622012-03-23 10:02:20 +0100102
Stephen Kitt67487032020-08-13 18:02:22 +0200103static const struct i2c_device_id mcp3021_id[];
104
105static int mcp3021_probe(struct i2c_client *client)
Xie Xiaobo5510e622012-03-23 10:02:20 +0100106{
107 int err;
108 struct mcp3021_data *data = NULL;
Clemens Gruberf4dc8112016-11-09 22:22:34 +0100109 struct device_node *np = client->dev.of_node;
Xie Xiaobo5510e622012-03-23 10:02:20 +0100110
111 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
112 return -ENODEV;
113
Guenter Roeck79831fd2012-06-02 11:20:19 -0700114 data = devm_kzalloc(&client->dev, sizeof(struct mcp3021_data),
115 GFP_KERNEL);
Xie Xiaobo5510e622012-03-23 10:02:20 +0100116 if (!data)
117 return -ENOMEM;
118
119 i2c_set_clientdata(client, data);
120
Clemens Gruberf4dc8112016-11-09 22:22:34 +0100121 if (np) {
122 if (!of_property_read_u32(np, "reference-voltage-microvolt",
123 &data->vdd))
124 data->vdd /= 1000;
125 else
126 data->vdd = MCP3021_VDD_REF_DEFAULT;
127 } else {
128 u32 *pdata = dev_get_platdata(&client->dev);
129
130 if (pdata)
131 data->vdd = *pdata;
132 else
133 data->vdd = MCP3021_VDD_REF_DEFAULT;
134 }
135
Stephen Kitt67487032020-08-13 18:02:22 +0200136 switch (i2c_match_id(mcp3021_id, client)->driver_data) {
Sven Schuchmann8b662f32012-09-21 13:04:21 +0200137 case mcp3021:
138 data->sar_shift = MCP3021_SAR_SHIFT;
139 data->sar_mask = MCP3021_SAR_MASK;
140 data->output_res = MCP3021_OUTPUT_RES;
Sven Schuchmann8b662f32012-09-21 13:04:21 +0200141 break;
Sven Schuchmann592758b2012-09-21 13:04:22 +0200142
143 case mcp3221:
144 data->sar_shift = MCP3221_SAR_SHIFT;
145 data->sar_mask = MCP3221_SAR_MASK;
146 data->output_res = MCP3221_OUTPUT_RES;
Sven Schuchmann592758b2012-09-21 13:04:22 +0200147 break;
Sven Schuchmann8b662f32012-09-21 13:04:21 +0200148 }
149
Clemens Gruberf4dc8112016-11-09 22:22:34 +0100150 if (data->vdd > MCP3021_VDD_REF_MAX || data->vdd < MCP3021_VDD_REF_MIN)
151 return -EINVAL;
Xie Xiaobo5510e622012-03-23 10:02:20 +0100152
153 err = sysfs_create_file(&client->dev.kobj, &dev_attr_in0_input.attr);
154 if (err)
Guenter Roeck79831fd2012-06-02 11:20:19 -0700155 return err;
Xie Xiaobo5510e622012-03-23 10:02:20 +0100156
157 data->hwmon_dev = hwmon_device_register(&client->dev);
158 if (IS_ERR(data->hwmon_dev)) {
159 err = PTR_ERR(data->hwmon_dev);
160 goto exit_remove;
161 }
162
163 return 0;
164
165exit_remove:
166 sysfs_remove_file(&client->dev.kobj, &dev_attr_in0_input.attr);
Xie Xiaobo5510e622012-03-23 10:02:20 +0100167 return err;
168}
169
170static int mcp3021_remove(struct i2c_client *client)
171{
172 struct mcp3021_data *data = i2c_get_clientdata(client);
173
174 hwmon_device_unregister(data->hwmon_dev);
175 sysfs_remove_file(&client->dev.kobj, &dev_attr_in0_input.attr);
Xie Xiaobo5510e622012-03-23 10:02:20 +0100176
177 return 0;
178}
179
180static const struct i2c_device_id mcp3021_id[] = {
Sven Schuchmann8b662f32012-09-21 13:04:21 +0200181 { "mcp3021", mcp3021 },
Sven Schuchmann592758b2012-09-21 13:04:22 +0200182 { "mcp3221", mcp3221 },
Xie Xiaobo5510e622012-03-23 10:02:20 +0100183 { }
184};
185MODULE_DEVICE_TABLE(i2c, mcp3021_id);
186
Clemens Gruberf4dc8112016-11-09 22:22:34 +0100187#ifdef CONFIG_OF
188static const struct of_device_id of_mcp3021_match[] = {
189 { .compatible = "microchip,mcp3021", .data = (void *)mcp3021 },
190 { .compatible = "microchip,mcp3221", .data = (void *)mcp3221 },
191 { }
192};
193MODULE_DEVICE_TABLE(of, of_mcp3021_match);
194#endif
195
Xie Xiaobo5510e622012-03-23 10:02:20 +0100196static struct i2c_driver mcp3021_driver = {
197 .driver = {
198 .name = "mcp3021",
Clemens Gruberf4dc8112016-11-09 22:22:34 +0100199 .of_match_table = of_match_ptr(of_mcp3021_match),
Xie Xiaobo5510e622012-03-23 10:02:20 +0100200 },
Stephen Kitt67487032020-08-13 18:02:22 +0200201 .probe_new = mcp3021_probe,
Xie Xiaobo5510e622012-03-23 10:02:20 +0100202 .remove = mcp3021_remove,
203 .id_table = mcp3021_id,
204};
205
206module_i2c_driver(mcp3021_driver);
207
208MODULE_AUTHOR("Mingkai Hu <Mingkai.hu@freescale.com>");
Sven Schuchmann592758b2012-09-21 13:04:22 +0200209MODULE_DESCRIPTION("Microchip MCP3021/MCP3221 driver");
Xie Xiaobo5510e622012-03-23 10:02:20 +0100210MODULE_LICENSE("GPL");