Linux Audio

Check our new training course

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