Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * mcp3021.c - driver for Microchip MCP3021 and MCP3221
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright (C) 2008-2009, 2012 Freescale Semiconductor, Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  * Author: Mingkai Hu <Mingkai.hu@freescale.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * Reworked by Sven Schuchmann <schuchmann@schleissheimer.de>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  * DT support added by Clemens Gruber <clemens.gruber@pqgruber.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  * This driver export the value of analog input voltage to sysfs, the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11)  * voltage unit is mV. Through the sysfs interface, lm-sensors tool
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12)  * can also display the input voltage.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/hwmon.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #include <linux/i2c.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include <linux/err.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #include <linux/device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #include <linux/of.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #include <linux/of_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) /* Vdd / reference voltage in millivolt */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) #define MCP3021_VDD_REF_MAX	5500
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) #define MCP3021_VDD_REF_MIN	2700
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) #define MCP3021_VDD_REF_DEFAULT	3300
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) /* output format */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) #define MCP3021_SAR_SHIFT	2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) #define MCP3021_SAR_MASK	0x3ff
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) #define MCP3021_OUTPUT_RES	10	/* 10-bit resolution */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) #define MCP3221_SAR_SHIFT	0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) #define MCP3221_SAR_MASK	0xfff
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) #define MCP3221_OUTPUT_RES	12	/* 12-bit resolution */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) enum chips {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	mcp3021,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	mcp3221
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45)  * Client data (each client gets its own)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) struct mcp3021_data {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	struct device *hwmon_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	u32 vdd;        /* supply and reference voltage in millivolt */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 	u16 sar_shift;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	u16 sar_mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	u8 output_res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) static int mcp3021_read16(struct i2c_client *client)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	struct mcp3021_data *data = i2c_get_clientdata(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	u16 reg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	__be16 buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	ret = i2c_master_recv(client, (char *)&buf, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	if (ret != 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 		return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	/* The output code of the MCP3021 is transmitted with MSB first. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	reg = be16_to_cpu(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	 * The ten-bit output code is composed of the lower 4-bit of the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 	 * first byte and the upper 6-bit of the second byte.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	reg = (reg >> data->sar_shift) & data->sar_mask;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	return reg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) static inline u16 volts_from_reg(struct mcp3021_data *data, u16 val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	return DIV_ROUND_CLOSEST(data->vdd * val, 1 << data->output_res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) static ssize_t in0_input_show(struct device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 			      struct device_attribute *attr, char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	struct i2c_client *client = to_i2c_client(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	struct mcp3021_data *data = i2c_get_clientdata(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	int reg, in_input;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	reg = mcp3021_read16(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	if (reg < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 		return reg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	in_input = volts_from_reg(data, reg);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	return sprintf(buf, "%d\n", in_input);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) static DEVICE_ATTR_RO(in0_input);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) static const struct i2c_device_id mcp3021_id[];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) static int mcp3021_probe(struct i2c_client *client)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	struct mcp3021_data *data = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	struct device_node *np = client->dev.of_node;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	data = devm_kzalloc(&client->dev, sizeof(struct mcp3021_data),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 			    GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	if (!data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	i2c_set_clientdata(client, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	if (np) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 		if (!of_property_read_u32(np, "reference-voltage-microvolt",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 					  &data->vdd))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 			data->vdd /= 1000;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 			data->vdd = MCP3021_VDD_REF_DEFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 		u32 *pdata = dev_get_platdata(&client->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 		if (pdata)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 			data->vdd = *pdata;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 			data->vdd = MCP3021_VDD_REF_DEFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 	switch (i2c_match_id(mcp3021_id, client)->driver_data) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 	case mcp3021:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 		data->sar_shift = MCP3021_SAR_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 		data->sar_mask = MCP3021_SAR_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 		data->output_res = MCP3021_OUTPUT_RES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 	case mcp3221:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 		data->sar_shift = MCP3221_SAR_SHIFT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 		data->sar_mask = MCP3221_SAR_MASK;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 		data->output_res = MCP3221_OUTPUT_RES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 	if (data->vdd > MCP3021_VDD_REF_MAX || data->vdd < MCP3021_VDD_REF_MIN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 	err = sysfs_create_file(&client->dev.kobj, &dev_attr_in0_input.attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 	data->hwmon_dev = hwmon_device_register(&client->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	if (IS_ERR(data->hwmon_dev)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) 		err = PTR_ERR(data->hwmon_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 		goto exit_remove;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) exit_remove:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 	sysfs_remove_file(&client->dev.kobj, &dev_attr_in0_input.attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) static int mcp3021_remove(struct i2c_client *client)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	struct mcp3021_data *data = i2c_get_clientdata(client);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	hwmon_device_unregister(data->hwmon_dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	sysfs_remove_file(&client->dev.kobj, &dev_attr_in0_input.attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) static const struct i2c_device_id mcp3021_id[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 	{ "mcp3021", mcp3021 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) 	{ "mcp3221", mcp3221 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) 	{ }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) MODULE_DEVICE_TABLE(i2c, mcp3021_id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) #ifdef CONFIG_OF
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) static const struct of_device_id of_mcp3021_match[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	{ .compatible = "microchip,mcp3021", .data = (void *)mcp3021 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 	{ .compatible = "microchip,mcp3221", .data = (void *)mcp3221 },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 	{ }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) MODULE_DEVICE_TABLE(of, of_mcp3021_match);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) static struct i2c_driver mcp3021_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 	.driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 		.name = "mcp3021",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 		.of_match_table = of_match_ptr(of_mcp3021_match),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 	},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 	.probe_new = mcp3021_probe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 	.remove = mcp3021_remove,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 	.id_table = mcp3021_id,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) module_i2c_driver(mcp3021_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) MODULE_AUTHOR("Mingkai Hu <Mingkai.hu@freescale.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) MODULE_DESCRIPTION("Microchip MCP3021/MCP3221 driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) MODULE_LICENSE("GPL");