11d0ea069SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
255b951e7SEric Miao /*
355b951e7SEric Miao * max1111.c - +2.7V, Low-Power, Multichannel, Serial 8-bit ADCs
455b951e7SEric Miao *
555b951e7SEric Miao * Based on arch/arm/mach-pxa/corgi_ssp.c
655b951e7SEric Miao *
755b951e7SEric Miao * Copyright (C) 2004-2005 Richard Purdie
855b951e7SEric Miao *
955b951e7SEric Miao * Copyright (C) 2008 Marvell International Ltd.
1055b951e7SEric Miao * Eric Miao <eric.miao@marvell.com>
1155b951e7SEric Miao */
1255b951e7SEric Miao
1355b951e7SEric Miao #include <linux/module.h>
1455b951e7SEric Miao #include <linux/kernel.h>
1555b951e7SEric Miao #include <linux/init.h>
1655b951e7SEric Miao #include <linux/err.h>
1755b951e7SEric Miao #include <linux/hwmon.h>
1855b951e7SEric Miao #include <linux/hwmon-sysfs.h>
1955b951e7SEric Miao #include <linux/spi/spi.h>
205a0e3ad6STejun Heo #include <linux/slab.h>
2155b951e7SEric Miao
229224c38aSGuenter Roeck enum chips { max1110, max1111, max1112, max1113 };
239224c38aSGuenter Roeck
2455b951e7SEric Miao #define MAX1111_TX_BUF_SIZE 1
2555b951e7SEric Miao #define MAX1111_RX_BUF_SIZE 2
2655b951e7SEric Miao
2755b951e7SEric Miao /* MAX1111 Commands */
2855b951e7SEric Miao #define MAX1111_CTRL_PD0 (1u << 0)
2955b951e7SEric Miao #define MAX1111_CTRL_PD1 (1u << 1)
3055b951e7SEric Miao #define MAX1111_CTRL_SGL (1u << 2)
3155b951e7SEric Miao #define MAX1111_CTRL_UNI (1u << 3)
329224c38aSGuenter Roeck #define MAX1110_CTRL_SEL_SH (4)
3355b951e7SEric Miao #define MAX1111_CTRL_SEL_SH (5) /* NOTE: bit 4 is ignored */
3455b951e7SEric Miao #define MAX1111_CTRL_STR (1u << 7)
3555b951e7SEric Miao
3655b951e7SEric Miao struct max1111_data {
3755b951e7SEric Miao struct spi_device *spi;
3855b951e7SEric Miao struct device *hwmon_dev;
3955b951e7SEric Miao struct spi_message msg;
4055b951e7SEric Miao struct spi_transfer xfer[2];
41d737c09cSJean Delvare uint8_t tx_buf[MAX1111_TX_BUF_SIZE];
42d737c09cSJean Delvare uint8_t rx_buf[MAX1111_RX_BUF_SIZE];
43d3f684f2SPavel Herrmann struct mutex drvdata_lock;
44d3f684f2SPavel Herrmann /* protect msg, xfer and buffers from multiple access */
459224c38aSGuenter Roeck int sel_sh;
469224c38aSGuenter Roeck int lsb;
4755b951e7SEric Miao };
4855b951e7SEric Miao
max1111_read(struct device * dev,int channel)4955b951e7SEric Miao static int max1111_read(struct device *dev, int channel)
5055b951e7SEric Miao {
5155b951e7SEric Miao struct max1111_data *data = dev_get_drvdata(dev);
5255b951e7SEric Miao uint8_t v1, v2;
5355b951e7SEric Miao int err;
5455b951e7SEric Miao
55d3f684f2SPavel Herrmann /* writing to drvdata struct is not thread safe, wait on mutex */
56d3f684f2SPavel Herrmann mutex_lock(&data->drvdata_lock);
57d3f684f2SPavel Herrmann
589224c38aSGuenter Roeck data->tx_buf[0] = (channel << data->sel_sh) |
5955b951e7SEric Miao MAX1111_CTRL_PD0 | MAX1111_CTRL_PD1 |
6055b951e7SEric Miao MAX1111_CTRL_SGL | MAX1111_CTRL_UNI | MAX1111_CTRL_STR;
6155b951e7SEric Miao
6255b951e7SEric Miao err = spi_sync(data->spi, &data->msg);
6355b951e7SEric Miao if (err < 0) {
6455b951e7SEric Miao dev_err(dev, "spi_sync failed with %d\n", err);
65d3f684f2SPavel Herrmann mutex_unlock(&data->drvdata_lock);
6655b951e7SEric Miao return err;
6755b951e7SEric Miao }
6855b951e7SEric Miao
6955b951e7SEric Miao v1 = data->rx_buf[0];
7055b951e7SEric Miao v2 = data->rx_buf[1];
7155b951e7SEric Miao
72d3f684f2SPavel Herrmann mutex_unlock(&data->drvdata_lock);
73d3f684f2SPavel Herrmann
7455b951e7SEric Miao if ((v1 & 0xc0) || (v2 & 0x3f))
7555b951e7SEric Miao return -EINVAL;
7655b951e7SEric Miao
7755b951e7SEric Miao return (v1 << 2) | (v2 >> 6);
7855b951e7SEric Miao }
7955b951e7SEric Miao
80f16177c2SEric Miao #ifdef CONFIG_SHARPSL_PM
81f16177c2SEric Miao static struct max1111_data *the_max1111;
82f16177c2SEric Miao
83*a9ae9c52SArnd Bergmann int max1111_read_channel(int channel);
max1111_read_channel(int channel)84f16177c2SEric Miao int max1111_read_channel(int channel)
85f16177c2SEric Miao {
863c2e2266SGuenter Roeck if (!the_max1111 || !the_max1111->spi)
873c2e2266SGuenter Roeck return -ENODEV;
883c2e2266SGuenter Roeck
89f16177c2SEric Miao return max1111_read(&the_max1111->spi->dev, channel);
90f16177c2SEric Miao }
91f16177c2SEric Miao EXPORT_SYMBOL(max1111_read_channel);
92f16177c2SEric Miao #endif
93f16177c2SEric Miao
9455b951e7SEric Miao /*
9555b951e7SEric Miao * NOTE: SPI devices do not have a default 'name' attribute, which is
9655b951e7SEric Miao * likely to be used by hwmon applications to distinguish between
9755b951e7SEric Miao * different devices, explicitly add a name attribute here.
9855b951e7SEric Miao */
name_show(struct device * dev,struct device_attribute * attr,char * buf)9917c45c4dSJulia Lawall static ssize_t name_show(struct device *dev,
10055b951e7SEric Miao struct device_attribute *attr, char *buf)
10155b951e7SEric Miao {
1029224c38aSGuenter Roeck return sprintf(buf, "%s\n", to_spi_device(dev)->modalias);
10355b951e7SEric Miao }
10455b951e7SEric Miao
show_adc(struct device * dev,struct device_attribute * attr,char * buf)10555b951e7SEric Miao static ssize_t show_adc(struct device *dev,
10655b951e7SEric Miao struct device_attribute *attr, char *buf)
10755b951e7SEric Miao {
1089224c38aSGuenter Roeck struct max1111_data *data = dev_get_drvdata(dev);
10955b951e7SEric Miao int channel = to_sensor_dev_attr(attr)->index;
11055b951e7SEric Miao int ret;
11155b951e7SEric Miao
11255b951e7SEric Miao ret = max1111_read(dev, channel);
11355b951e7SEric Miao if (ret < 0)
11455b951e7SEric Miao return ret;
11555b951e7SEric Miao
11651683ee8SGuenter Roeck /*
1179224c38aSGuenter Roeck * Assume the reference voltage to be 2.048V or 4.096V, with an 8-bit
1189224c38aSGuenter Roeck * sample. The LSB weight is 8mV or 16mV depending on the chip type.
119012f3b91SEric Miao */
1209224c38aSGuenter Roeck return sprintf(buf, "%d\n", ret * data->lsb);
12155b951e7SEric Miao }
12255b951e7SEric Miao
12355b951e7SEric Miao #define MAX1111_ADC_ATTR(_id) \
124012f3b91SEric Miao SENSOR_DEVICE_ATTR(in##_id##_input, S_IRUGO, show_adc, NULL, _id)
12555b951e7SEric Miao
12617c45c4dSJulia Lawall static DEVICE_ATTR_RO(name);
12755b951e7SEric Miao static MAX1111_ADC_ATTR(0);
12855b951e7SEric Miao static MAX1111_ADC_ATTR(1);
12955b951e7SEric Miao static MAX1111_ADC_ATTR(2);
13055b951e7SEric Miao static MAX1111_ADC_ATTR(3);
1319224c38aSGuenter Roeck static MAX1111_ADC_ATTR(4);
1329224c38aSGuenter Roeck static MAX1111_ADC_ATTR(5);
1339224c38aSGuenter Roeck static MAX1111_ADC_ATTR(6);
1349224c38aSGuenter Roeck static MAX1111_ADC_ATTR(7);
13555b951e7SEric Miao
13655b951e7SEric Miao static struct attribute *max1111_attributes[] = {
13755b951e7SEric Miao &dev_attr_name.attr,
138012f3b91SEric Miao &sensor_dev_attr_in0_input.dev_attr.attr,
139012f3b91SEric Miao &sensor_dev_attr_in1_input.dev_attr.attr,
140012f3b91SEric Miao &sensor_dev_attr_in2_input.dev_attr.attr,
141012f3b91SEric Miao &sensor_dev_attr_in3_input.dev_attr.attr,
14255b951e7SEric Miao NULL,
14355b951e7SEric Miao };
14455b951e7SEric Miao
14555b951e7SEric Miao static const struct attribute_group max1111_attr_group = {
14655b951e7SEric Miao .attrs = max1111_attributes,
14755b951e7SEric Miao };
14855b951e7SEric Miao
1499224c38aSGuenter Roeck static struct attribute *max1110_attributes[] = {
1509224c38aSGuenter Roeck &sensor_dev_attr_in4_input.dev_attr.attr,
1519224c38aSGuenter Roeck &sensor_dev_attr_in5_input.dev_attr.attr,
1529224c38aSGuenter Roeck &sensor_dev_attr_in6_input.dev_attr.attr,
1539224c38aSGuenter Roeck &sensor_dev_attr_in7_input.dev_attr.attr,
1549224c38aSGuenter Roeck NULL,
1559224c38aSGuenter Roeck };
1569224c38aSGuenter Roeck
1579224c38aSGuenter Roeck static const struct attribute_group max1110_attr_group = {
1589224c38aSGuenter Roeck .attrs = max1110_attributes,
1599224c38aSGuenter Roeck };
1609224c38aSGuenter Roeck
setup_transfer(struct max1111_data * data)1616c931ae1SBill Pemberton static int setup_transfer(struct max1111_data *data)
16255b951e7SEric Miao {
16355b951e7SEric Miao struct spi_message *m;
16455b951e7SEric Miao struct spi_transfer *x;
16555b951e7SEric Miao
16655b951e7SEric Miao m = &data->msg;
16755b951e7SEric Miao x = &data->xfer[0];
16855b951e7SEric Miao
16955b951e7SEric Miao spi_message_init(m);
17055b951e7SEric Miao
17155b951e7SEric Miao x->tx_buf = &data->tx_buf[0];
172d737c09cSJean Delvare x->len = MAX1111_TX_BUF_SIZE;
17355b951e7SEric Miao spi_message_add_tail(x, m);
17455b951e7SEric Miao
17555b951e7SEric Miao x++;
17655b951e7SEric Miao x->rx_buf = &data->rx_buf[0];
177d737c09cSJean Delvare x->len = MAX1111_RX_BUF_SIZE;
17855b951e7SEric Miao spi_message_add_tail(x, m);
17955b951e7SEric Miao
18055b951e7SEric Miao return 0;
18155b951e7SEric Miao }
18255b951e7SEric Miao
max1111_probe(struct spi_device * spi)1836c931ae1SBill Pemberton static int max1111_probe(struct spi_device *spi)
18455b951e7SEric Miao {
1859224c38aSGuenter Roeck enum chips chip = spi_get_device_id(spi)->driver_data;
18655b951e7SEric Miao struct max1111_data *data;
18755b951e7SEric Miao int err;
18855b951e7SEric Miao
18955b951e7SEric Miao spi->bits_per_word = 8;
19055b951e7SEric Miao spi->mode = SPI_MODE_0;
19155b951e7SEric Miao err = spi_setup(spi);
19255b951e7SEric Miao if (err < 0)
19355b951e7SEric Miao return err;
19455b951e7SEric Miao
195661f020fSGuenter Roeck data = devm_kzalloc(&spi->dev, sizeof(struct max1111_data), GFP_KERNEL);
196611cd8aeSJingoo Han if (data == NULL)
19755b951e7SEric Miao return -ENOMEM;
19855b951e7SEric Miao
1999224c38aSGuenter Roeck switch (chip) {
2009224c38aSGuenter Roeck case max1110:
2019224c38aSGuenter Roeck data->lsb = 8;
2029224c38aSGuenter Roeck data->sel_sh = MAX1110_CTRL_SEL_SH;
2039224c38aSGuenter Roeck break;
2049224c38aSGuenter Roeck case max1111:
2059224c38aSGuenter Roeck data->lsb = 8;
2069224c38aSGuenter Roeck data->sel_sh = MAX1111_CTRL_SEL_SH;
2079224c38aSGuenter Roeck break;
2089224c38aSGuenter Roeck case max1112:
2099224c38aSGuenter Roeck data->lsb = 16;
2109224c38aSGuenter Roeck data->sel_sh = MAX1110_CTRL_SEL_SH;
2119224c38aSGuenter Roeck break;
2129224c38aSGuenter Roeck case max1113:
2139224c38aSGuenter Roeck data->lsb = 16;
2149224c38aSGuenter Roeck data->sel_sh = MAX1111_CTRL_SEL_SH;
2159224c38aSGuenter Roeck break;
2169224c38aSGuenter Roeck }
21755b951e7SEric Miao err = setup_transfer(data);
21855b951e7SEric Miao if (err)
219661f020fSGuenter Roeck return err;
22055b951e7SEric Miao
221d3f684f2SPavel Herrmann mutex_init(&data->drvdata_lock);
222d3f684f2SPavel Herrmann
22355b951e7SEric Miao data->spi = spi;
22455b951e7SEric Miao spi_set_drvdata(spi, data);
22555b951e7SEric Miao
22655b951e7SEric Miao err = sysfs_create_group(&spi->dev.kobj, &max1111_attr_group);
22755b951e7SEric Miao if (err) {
22855b951e7SEric Miao dev_err(&spi->dev, "failed to create attribute group\n");
229661f020fSGuenter Roeck return err;
23055b951e7SEric Miao }
2319224c38aSGuenter Roeck if (chip == max1110 || chip == max1112) {
2329224c38aSGuenter Roeck err = sysfs_create_group(&spi->dev.kobj, &max1110_attr_group);
2339224c38aSGuenter Roeck if (err) {
2349224c38aSGuenter Roeck dev_err(&spi->dev,
2359224c38aSGuenter Roeck "failed to create extended attribute group\n");
2369224c38aSGuenter Roeck goto err_remove;
2379224c38aSGuenter Roeck }
2389224c38aSGuenter Roeck }
23955b951e7SEric Miao
24055b951e7SEric Miao data->hwmon_dev = hwmon_device_register(&spi->dev);
24155b951e7SEric Miao if (IS_ERR(data->hwmon_dev)) {
24255b951e7SEric Miao dev_err(&spi->dev, "failed to create hwmon device\n");
24355b951e7SEric Miao err = PTR_ERR(data->hwmon_dev);
24455b951e7SEric Miao goto err_remove;
24555b951e7SEric Miao }
24655b951e7SEric Miao
247f16177c2SEric Miao #ifdef CONFIG_SHARPSL_PM
248f16177c2SEric Miao the_max1111 = data;
249f16177c2SEric Miao #endif
25055b951e7SEric Miao return 0;
25155b951e7SEric Miao
25255b951e7SEric Miao err_remove:
2539224c38aSGuenter Roeck sysfs_remove_group(&spi->dev.kobj, &max1110_attr_group);
25455b951e7SEric Miao sysfs_remove_group(&spi->dev.kobj, &max1111_attr_group);
25555b951e7SEric Miao return err;
25655b951e7SEric Miao }
25755b951e7SEric Miao
max1111_remove(struct spi_device * spi)258a0386bbaSUwe Kleine-König static void max1111_remove(struct spi_device *spi)
25955b951e7SEric Miao {
26055b951e7SEric Miao struct max1111_data *data = spi_get_drvdata(spi);
26155b951e7SEric Miao
2623c2e2266SGuenter Roeck #ifdef CONFIG_SHARPSL_PM
2633c2e2266SGuenter Roeck the_max1111 = NULL;
2643c2e2266SGuenter Roeck #endif
26555b951e7SEric Miao hwmon_device_unregister(data->hwmon_dev);
2669224c38aSGuenter Roeck sysfs_remove_group(&spi->dev.kobj, &max1110_attr_group);
26755b951e7SEric Miao sysfs_remove_group(&spi->dev.kobj, &max1111_attr_group);
268d3f684f2SPavel Herrmann mutex_destroy(&data->drvdata_lock);
26955b951e7SEric Miao }
27055b951e7SEric Miao
2719224c38aSGuenter Roeck static const struct spi_device_id max1111_ids[] = {
2729224c38aSGuenter Roeck { "max1110", max1110 },
2739224c38aSGuenter Roeck { "max1111", max1111 },
2749224c38aSGuenter Roeck { "max1112", max1112 },
2759224c38aSGuenter Roeck { "max1113", max1113 },
2769224c38aSGuenter Roeck { },
2779224c38aSGuenter Roeck };
2789224c38aSGuenter Roeck MODULE_DEVICE_TABLE(spi, max1111_ids);
2799224c38aSGuenter Roeck
28055b951e7SEric Miao static struct spi_driver max1111_driver = {
28155b951e7SEric Miao .driver = {
28255b951e7SEric Miao .name = "max1111",
28355b951e7SEric Miao },
2849224c38aSGuenter Roeck .id_table = max1111_ids,
28555b951e7SEric Miao .probe = max1111_probe,
2869e5e9b7aSBill Pemberton .remove = max1111_remove,
28755b951e7SEric Miao };
28855b951e7SEric Miao
28991efffe2SAxel Lin module_spi_driver(max1111_driver);
29055b951e7SEric Miao
29155b951e7SEric Miao MODULE_AUTHOR("Eric Miao <eric.miao@marvell.com>");
2929224c38aSGuenter Roeck MODULE_DESCRIPTION("MAX1110/MAX1111/MAX1112/MAX1113 ADC Driver");
29355b951e7SEric Miao MODULE_LICENSE("GPL");
294