blob: eedb32292d6d3c20035f56a90820861d38e02102 [file] [log] [blame]
Xie Xiaobo5510e622012-03-23 10:02:20 +01001/*
Sven Schuchmann592758b2012-09-21 13:04:22 +02002 * mcp3021.c - driver for Microchip MCP3021 and MCP3221
Xie Xiaobo5510e622012-03-23 10:02:20 +01003 *
4 * Copyright (C) 2008-2009, 2012 Freescale Semiconductor, Inc.
5 * Author: Mingkai Hu <Mingkai.hu@freescale.com>
Sven Schuchmann8b662f32012-09-21 13:04:21 +02006 * Reworked by Sven Schuchmann <schuchmann@schleissheimer.de>
Xie Xiaobo5510e622012-03-23 10:02:20 +01007 *
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
35#define MCP3021_OUTPUT_RES 10 /* 10-bit resolution */
36#define MCP3021_OUTPUT_SCALE 4
37
Sven Schuchmann592758b2012-09-21 13:04:22 +020038#define MCP3221_SAR_SHIFT 0
39#define MCP3221_SAR_MASK 0xfff
40#define MCP3221_OUTPUT_RES 12 /* 12-bit resolution */
41#define MCP3221_OUTPUT_SCALE 1
42
Sven Schuchmann8b662f32012-09-21 13:04:21 +020043enum chips {
Sven Schuchmann592758b2012-09-21 13:04:22 +020044 mcp3021,
45 mcp3221
Sven Schuchmann8b662f32012-09-21 13:04:21 +020046};
Sven Schuchmann592758b2012-09-21 13:04:22 +020047
Xie Xiaobo5510e622012-03-23 10:02:20 +010048/*
49 * Client data (each client gets its own)
50 */
51struct mcp3021_data {
52 struct device *hwmon_dev;
53 u32 vdd; /* device power supply */
Sven Schuchmann8b662f32012-09-21 13:04:21 +020054 u16 sar_shift;
55 u16 sar_mask;
56 u8 output_res;
57 u8 output_scale;
Xie Xiaobo5510e622012-03-23 10:02:20 +010058};
59
60static int mcp3021_read16(struct i2c_client *client)
61{
Sven Schuchmann8b662f32012-09-21 13:04:21 +020062 struct mcp3021_data *data = i2c_get_clientdata(client);
Xie Xiaobo5510e622012-03-23 10:02:20 +010063 int ret;
64 u16 reg;
65 __be16 buf;
66
67 ret = i2c_master_recv(client, (char *)&buf, 2);
68 if (ret < 0)
69 return ret;
70 if (ret != 2)
71 return -EIO;
72
73 /* The output code of the MCP3021 is transmitted with MSB first. */
74 reg = be16_to_cpu(buf);
75
76 /*
77 * The ten-bit output code is composed of the lower 4-bit of the
78 * first byte and the upper 6-bit of the second byte.
79 */
Sven Schuchmann8b662f32012-09-21 13:04:21 +020080 reg = (reg >> data->sar_shift) & data->sar_mask;
Xie Xiaobo5510e622012-03-23 10:02:20 +010081
82 return reg;
83}
84
Sven Schuchmann8b662f32012-09-21 13:04:21 +020085static inline u16 volts_from_reg(struct mcp3021_data *data, u16 val)
Xie Xiaobo5510e622012-03-23 10:02:20 +010086{
87 if (val == 0)
88 return 0;
89
Sven Schuchmann8b662f32012-09-21 13:04:21 +020090 val = val * data->output_scale - data->output_scale / 2;
Xie Xiaobo5510e622012-03-23 10:02:20 +010091
Sven Schuchmann8b662f32012-09-21 13:04:21 +020092 return val * DIV_ROUND_CLOSEST(data->vdd,
93 (1 << data->output_res) * data->output_scale);
Xie Xiaobo5510e622012-03-23 10:02:20 +010094}
95
96static ssize_t show_in_input(struct device *dev, struct device_attribute *attr,
97 char *buf)
98{
99 struct i2c_client *client = to_i2c_client(dev);
100 struct mcp3021_data *data = i2c_get_clientdata(client);
101 int reg, in_input;
102
103 reg = mcp3021_read16(client);
104 if (reg < 0)
105 return reg;
106
Sven Schuchmann8b662f32012-09-21 13:04:21 +0200107 in_input = volts_from_reg(data, reg);
108
Xie Xiaobo5510e622012-03-23 10:02:20 +0100109 return sprintf(buf, "%d\n", in_input);
110}
111
112static DEVICE_ATTR(in0_input, S_IRUGO, show_in_input, NULL);
113
114static int mcp3021_probe(struct i2c_client *client,
115 const struct i2c_device_id *id)
116{
117 int err;
118 struct mcp3021_data *data = NULL;
119
120 if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
121 return -ENODEV;
122
Guenter Roeck79831fd2012-06-02 11:20:19 -0700123 data = devm_kzalloc(&client->dev, sizeof(struct mcp3021_data),
124 GFP_KERNEL);
Xie Xiaobo5510e622012-03-23 10:02:20 +0100125 if (!data)
126 return -ENOMEM;
127
128 i2c_set_clientdata(client, data);
129
Sven Schuchmann8b662f32012-09-21 13:04:21 +0200130 switch (id->driver_data) {
131 case mcp3021:
132 data->sar_shift = MCP3021_SAR_SHIFT;
133 data->sar_mask = MCP3021_SAR_MASK;
134 data->output_res = MCP3021_OUTPUT_RES;
135 data->output_scale = MCP3021_OUTPUT_SCALE;
136 break;
Sven Schuchmann592758b2012-09-21 13:04:22 +0200137
138 case mcp3221:
139 data->sar_shift = MCP3221_SAR_SHIFT;
140 data->sar_mask = MCP3221_SAR_MASK;
141 data->output_res = MCP3221_OUTPUT_RES;
142 data->output_scale = MCP3221_OUTPUT_SCALE;
143 break;
Sven Schuchmann8b662f32012-09-21 13:04:21 +0200144 }
145
Xie Xiaobo5510e622012-03-23 10:02:20 +0100146 if (client->dev.platform_data) {
147 data->vdd = *(u32 *)client->dev.platform_data;
Guenter Roeck79831fd2012-06-02 11:20:19 -0700148 if (data->vdd > MCP3021_VDD_MAX || data->vdd < MCP3021_VDD_MIN)
149 return -EINVAL;
Xie Xiaobo5510e622012-03-23 10:02:20 +0100150 } else
151 data->vdd = MCP3021_VDD_REF;
152
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
187static struct i2c_driver mcp3021_driver = {
188 .driver = {
189 .name = "mcp3021",
190 },
191 .probe = mcp3021_probe,
192 .remove = mcp3021_remove,
193 .id_table = mcp3021_id,
194};
195
196module_i2c_driver(mcp3021_driver);
197
198MODULE_AUTHOR("Mingkai Hu <Mingkai.hu@freescale.com>");
Sven Schuchmann592758b2012-09-21 13:04:22 +0200199MODULE_DESCRIPTION("Microchip MCP3021/MCP3221 driver");
Xie Xiaobo5510e622012-03-23 10:02:20 +0100200MODULE_LICENSE("GPL");