2 * mcp3021.c - driver for Microchip MCP3021 and MCP3221
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 * DT support added by Clemens Gruber <clemens.gruber@pqgruber.com>
9 * This driver export the value of analog input voltage to sysfs, the
10 * voltage unit is mV. Through the sysfs interface, lm-sensors tool
11 * can also display the input voltage.
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
19 #include <linux/kernel.h>
20 #include <linux/module.h>
21 #include <linux/hwmon.h>
22 #include <linux/slab.h>
23 #include <linux/i2c.h>
24 #include <linux/err.h>
25 #include <linux/device.h>
27 #include <linux/of_device.h>
29 /* Vdd / reference voltage in millivolt */
30 #define MCP3021_VDD_REF_MAX 5500
31 #define MCP3021_VDD_REF_MIN 2700
32 #define MCP3021_VDD_REF_DEFAULT 3300
35 #define MCP3021_SAR_SHIFT 2
36 #define MCP3021_SAR_MASK 0x3ff
37 #define MCP3021_OUTPUT_RES 10 /* 10-bit resolution */
39 #define MCP3221_SAR_SHIFT 0
40 #define MCP3221_SAR_MASK 0xfff
41 #define MCP3221_OUTPUT_RES 12 /* 12-bit resolution */
49 * Client data (each client gets its own)
52 struct device
*hwmon_dev
;
53 u32 vdd
; /* supply and reference voltage in millivolt */
59 static int mcp3021_read16(struct i2c_client
*client
)
61 struct mcp3021_data
*data
= i2c_get_clientdata(client
);
66 ret
= i2c_master_recv(client
, (char *)&buf
, 2);
72 /* The output code of the MCP3021 is transmitted with MSB first. */
73 reg
= be16_to_cpu(buf
);
76 * The ten-bit output code is composed of the lower 4-bit of the
77 * first byte and the upper 6-bit of the second byte.
79 reg
= (reg
>> data
->sar_shift
) & data
->sar_mask
;
84 static inline u16
volts_from_reg(struct mcp3021_data
*data
, u16 val
)
86 return DIV_ROUND_CLOSEST(data
->vdd
* val
, 1 << data
->output_res
);
89 static ssize_t
in0_input_show(struct device
*dev
,
90 struct device_attribute
*attr
, char *buf
)
92 struct i2c_client
*client
= to_i2c_client(dev
);
93 struct mcp3021_data
*data
= i2c_get_clientdata(client
);
96 reg
= mcp3021_read16(client
);
100 in_input
= volts_from_reg(data
, reg
);
102 return sprintf(buf
, "%d\n", in_input
);
105 static DEVICE_ATTR_RO(in0_input
);
107 static int mcp3021_probe(struct i2c_client
*client
,
108 const struct i2c_device_id
*id
)
111 struct mcp3021_data
*data
= NULL
;
112 struct device_node
*np
= client
->dev
.of_node
;
114 if (!i2c_check_functionality(client
->adapter
, I2C_FUNC_I2C
))
117 data
= devm_kzalloc(&client
->dev
, sizeof(struct mcp3021_data
),
122 i2c_set_clientdata(client
, data
);
125 if (!of_property_read_u32(np
, "reference-voltage-microvolt",
129 data
->vdd
= MCP3021_VDD_REF_DEFAULT
;
131 u32
*pdata
= dev_get_platdata(&client
->dev
);
136 data
->vdd
= MCP3021_VDD_REF_DEFAULT
;
139 switch (id
->driver_data
) {
141 data
->sar_shift
= MCP3021_SAR_SHIFT
;
142 data
->sar_mask
= MCP3021_SAR_MASK
;
143 data
->output_res
= MCP3021_OUTPUT_RES
;
147 data
->sar_shift
= MCP3221_SAR_SHIFT
;
148 data
->sar_mask
= MCP3221_SAR_MASK
;
149 data
->output_res
= MCP3221_OUTPUT_RES
;
153 if (data
->vdd
> MCP3021_VDD_REF_MAX
|| data
->vdd
< MCP3021_VDD_REF_MIN
)
156 err
= sysfs_create_file(&client
->dev
.kobj
, &dev_attr_in0_input
.attr
);
160 data
->hwmon_dev
= hwmon_device_register(&client
->dev
);
161 if (IS_ERR(data
->hwmon_dev
)) {
162 err
= PTR_ERR(data
->hwmon_dev
);
169 sysfs_remove_file(&client
->dev
.kobj
, &dev_attr_in0_input
.attr
);
173 static int mcp3021_remove(struct i2c_client
*client
)
175 struct mcp3021_data
*data
= i2c_get_clientdata(client
);
177 hwmon_device_unregister(data
->hwmon_dev
);
178 sysfs_remove_file(&client
->dev
.kobj
, &dev_attr_in0_input
.attr
);
183 static const struct i2c_device_id mcp3021_id
[] = {
184 { "mcp3021", mcp3021
},
185 { "mcp3221", mcp3221
},
188 MODULE_DEVICE_TABLE(i2c
, mcp3021_id
);
191 static const struct of_device_id of_mcp3021_match
[] = {
192 { .compatible
= "microchip,mcp3021", .data
= (void *)mcp3021
},
193 { .compatible
= "microchip,mcp3221", .data
= (void *)mcp3221
},
196 MODULE_DEVICE_TABLE(of
, of_mcp3021_match
);
199 static struct i2c_driver mcp3021_driver
= {
202 .of_match_table
= of_match_ptr(of_mcp3021_match
),
204 .probe
= mcp3021_probe
,
205 .remove
= mcp3021_remove
,
206 .id_table
= mcp3021_id
,
209 module_i2c_driver(mcp3021_driver
);
211 MODULE_AUTHOR("Mingkai Hu <Mingkai.hu@freescale.com>");
212 MODULE_DESCRIPTION("Microchip MCP3021/MCP3221 driver");
213 MODULE_LICENSE("GPL");