Linux Audio

Check our new training course

Loading...
v6.13.7
 1// SPDX-License-Identifier: GPL-2.0-or-later
 2/*
 3 * AD7879/AD7889 touchscreen (SPI bus)
 4 *
 5 * Copyright (C) 2008-2010 Michael Hennerich, Analog Devices Inc.
 
 
 6 */
 7
 8#include <linux/input.h>	/* BUS_SPI */
 9#include <linux/pm.h>
10#include <linux/spi/spi.h>
11#include <linux/module.h>
12#include <linux/of.h>
13#include <linux/regmap.h>
14
15#include "ad7879.h"
16
17#define AD7879_DEVID		0x7A	/* AD7879/AD7889 */
18
19#define MAX_SPI_FREQ_HZ      5000000
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
21#define AD7879_CMD_MAGIC     0xE0
22#define AD7879_CMD_READ      BIT(2)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
24static const struct regmap_config ad7879_spi_regmap_config = {
25	.reg_bits = 16,
26	.val_bits = 16,
27	.max_register = 15,
28	.read_flag_mask = AD7879_CMD_MAGIC | AD7879_CMD_READ,
29	.write_flag_mask = AD7879_CMD_MAGIC,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30};
31
32static int ad7879_spi_probe(struct spi_device *spi)
33{
34	struct regmap *regmap;
 
35
36	/* don't exceed max specified SPI CLK frequency */
37	if (spi->max_speed_hz > MAX_SPI_FREQ_HZ) {
38		dev_err(&spi->dev, "SPI CLK %d Hz?\n", spi->max_speed_hz);
39		return -EINVAL;
40	}
41
42	regmap = devm_regmap_init_spi(spi, &ad7879_spi_regmap_config);
43	if (IS_ERR(regmap))
44		return PTR_ERR(regmap);
 
 
 
 
 
 
 
45
46	return ad7879_probe(&spi->dev, regmap, spi->irq, BUS_SPI, AD7879_DEVID);
 
 
47}
48
49#ifdef CONFIG_OF
50static const struct of_device_id ad7879_spi_dt_ids[] = {
51	{ .compatible = "adi,ad7879", },
52	{ }
53};
54MODULE_DEVICE_TABLE(of, ad7879_spi_dt_ids);
55#endif
 
 
56
57static struct spi_driver ad7879_spi_driver = {
58	.driver = {
59		.name		= "ad7879",
60		.dev_groups	= ad7879_groups,
61		.pm		= &ad7879_pm_ops,
62		.of_match_table	= of_match_ptr(ad7879_spi_dt_ids),
63	},
64	.probe		= ad7879_spi_probe,
 
65};
66
67module_spi_driver(ad7879_spi_driver);
 
 
 
 
 
 
 
 
 
 
68
69MODULE_AUTHOR("Michael Hennerich <michael.hennerich@analog.com>");
70MODULE_DESCRIPTION("AD7879(-1) touchscreen SPI bus driver");
71MODULE_LICENSE("GPL");
72MODULE_ALIAS("spi:ad7879");
v3.1
 
  1/*
  2 * AD7879/AD7889 touchscreen (SPI bus)
  3 *
  4 * Copyright (C) 2008-2010 Michael Hennerich, Analog Devices Inc.
  5 *
  6 * Licensed under the GPL-2 or later.
  7 */
  8
  9#include <linux/input.h>	/* BUS_SPI */
 10#include <linux/pm.h>
 11#include <linux/spi/spi.h>
 
 
 
 12
 13#include "ad7879.h"
 14
 15#define AD7879_DEVID		0x7A	/* AD7879/AD7889 */
 16
 17#define MAX_SPI_FREQ_HZ      5000000
 18#define AD7879_CMD_MAGIC     0xE000
 19#define AD7879_CMD_READ      (1 << 10)
 20#define AD7879_CMD(reg)      (AD7879_CMD_MAGIC | ((reg) & 0xF))
 21#define AD7879_WRITECMD(reg) (AD7879_CMD(reg))
 22#define AD7879_READCMD(reg)  (AD7879_CMD(reg) | AD7879_CMD_READ)
 23
 24#ifdef CONFIG_PM_SLEEP
 25static int ad7879_spi_suspend(struct device *dev)
 26{
 27	struct spi_device *spi = to_spi_device(dev);
 28	struct ad7879 *ts = spi_get_drvdata(spi);
 29
 30	ad7879_suspend(ts);
 31
 32	return 0;
 33}
 34
 35static int ad7879_spi_resume(struct device *dev)
 36{
 37	struct spi_device *spi = to_spi_device(dev);
 38	struct ad7879 *ts = spi_get_drvdata(spi);
 39
 40	ad7879_resume(ts);
 41
 42	return 0;
 43}
 44#endif
 45
 46static SIMPLE_DEV_PM_OPS(ad7879_spi_pm, ad7879_spi_suspend, ad7879_spi_resume);
 47
 48/*
 49 * ad7879_read/write are only used for initial setup and for sysfs controls.
 50 * The main traffic is done in ad7879_collect().
 51 */
 52
 53static int ad7879_spi_xfer(struct spi_device *spi,
 54			   u16 cmd, u8 count, u16 *tx_buf, u16 *rx_buf)
 55{
 56	struct spi_message msg;
 57	struct spi_transfer *xfers;
 58	void *spi_data;
 59	u16 *command;
 60	u16 *_rx_buf = _rx_buf; /* shut gcc up */
 61	u8 idx;
 62	int ret;
 63
 64	xfers = spi_data = kzalloc(sizeof(*xfers) * (count + 2), GFP_KERNEL);
 65	if (!spi_data)
 66		return -ENOMEM;
 67
 68	spi_message_init(&msg);
 69
 70	command = spi_data;
 71	command[0] = cmd;
 72	if (count == 1) {
 73		/* ad7879_spi_{read,write} gave us buf on stack */
 74		command[1] = *tx_buf;
 75		tx_buf = &command[1];
 76		_rx_buf = rx_buf;
 77		rx_buf = &command[2];
 78	}
 79
 80	++xfers;
 81	xfers[0].tx_buf = command;
 82	xfers[0].len = 2;
 83	spi_message_add_tail(&xfers[0], &msg);
 84	++xfers;
 85
 86	for (idx = 0; idx < count; ++idx) {
 87		if (rx_buf)
 88			xfers[idx].rx_buf = &rx_buf[idx];
 89		if (tx_buf)
 90			xfers[idx].tx_buf = &tx_buf[idx];
 91		xfers[idx].len = 2;
 92		spi_message_add_tail(&xfers[idx], &msg);
 93	}
 94
 95	ret = spi_sync(spi, &msg);
 96
 97	if (count == 1)
 98		_rx_buf[0] = command[2];
 99
100	kfree(spi_data);
101
102	return ret;
103}
104
105static int ad7879_spi_multi_read(struct device *dev,
106				 u8 first_reg, u8 count, u16 *buf)
107{
108	struct spi_device *spi = to_spi_device(dev);
109
110	return ad7879_spi_xfer(spi, AD7879_READCMD(first_reg), count, NULL, buf);
111}
112
113static int ad7879_spi_read(struct device *dev, u8 reg)
114{
115	struct spi_device *spi = to_spi_device(dev);
116	u16 ret, dummy;
117
118	return ad7879_spi_xfer(spi, AD7879_READCMD(reg), 1, &dummy, &ret) ? : ret;
119}
120
121static int ad7879_spi_write(struct device *dev, u8 reg, u16 val)
122{
123	struct spi_device *spi = to_spi_device(dev);
124	u16 dummy;
125
126	return ad7879_spi_xfer(spi, AD7879_WRITECMD(reg), 1, &val, &dummy);
127}
128
129static const struct ad7879_bus_ops ad7879_spi_bus_ops = {
130	.bustype	= BUS_SPI,
131	.read		= ad7879_spi_read,
132	.multi_read	= ad7879_spi_multi_read,
133	.write		= ad7879_spi_write,
134};
135
136static int __devinit ad7879_spi_probe(struct spi_device *spi)
137{
138	struct ad7879 *ts;
139	int err;
140
141	/* don't exceed max specified SPI CLK frequency */
142	if (spi->max_speed_hz > MAX_SPI_FREQ_HZ) {
143		dev_err(&spi->dev, "SPI CLK %d Hz?\n", spi->max_speed_hz);
144		return -EINVAL;
145	}
146
147	spi->bits_per_word = 16;
148	err = spi_setup(spi);
149	if (err) {
150	        dev_dbg(&spi->dev, "spi master doesn't support 16 bits/word\n");
151	        return err;
152	}
153
154	ts = ad7879_probe(&spi->dev, AD7879_DEVID, spi->irq, &ad7879_spi_bus_ops);
155	if (IS_ERR(ts))
156		return PTR_ERR(ts);
157
158	spi_set_drvdata(spi, ts);
159
160	return 0;
161}
162
163static int __devexit ad7879_spi_remove(struct spi_device *spi)
164{
165	struct ad7879 *ts = spi_get_drvdata(spi);
166
167	ad7879_remove(ts);
168	spi_set_drvdata(spi, NULL);
169
170	return 0;
171}
172
173static struct spi_driver ad7879_spi_driver = {
174	.driver = {
175		.name	= "ad7879",
176		.bus	= &spi_bus_type,
177		.owner	= THIS_MODULE,
178		.pm	= &ad7879_spi_pm,
179	},
180	.probe		= ad7879_spi_probe,
181	.remove		= __devexit_p(ad7879_spi_remove),
182};
183
184static int __init ad7879_spi_init(void)
185{
186	return spi_register_driver(&ad7879_spi_driver);
187}
188module_init(ad7879_spi_init);
189
190static void __exit ad7879_spi_exit(void)
191{
192	spi_unregister_driver(&ad7879_spi_driver);
193}
194module_exit(ad7879_spi_exit);
195
196MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
197MODULE_DESCRIPTION("AD7879(-1) touchscreen SPI bus driver");
198MODULE_LICENSE("GPL");
199MODULE_ALIAS("spi:ad7879");