xref: /openbmc/linux/drivers/input/touchscreen/ad7879-spi.c (revision 404a24c35db8c44dce91010023f12b73f2f44441)
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 #include <linux/module.h>
13 #include <linux/of.h>
14 #include <linux/regmap.h>
15 
16 #include "ad7879.h"
17 
18 #define AD7879_DEVID		0x7A	/* AD7879/AD7889 */
19 
20 #define MAX_SPI_FREQ_HZ      5000000
21 
22 #define AD7879_CMD_MAGIC     0xE0
23 #define AD7879_CMD_READ      BIT(2)
24 
25 static const struct regmap_config ad7879_spi_regmap_config = {
26 	.reg_bits = 16,
27 	.val_bits = 16,
28 	.max_register = 15,
29 	.read_flag_mask = AD7879_CMD_MAGIC | AD7879_CMD_READ,
30 	.write_flag_mask = AD7879_CMD_MAGIC,
31 };
32 
33 static int ad7879_spi_probe(struct spi_device *spi)
34 {
35 	struct ad7879 *ts;
36 	struct regmap *regmap;
37 	int err;
38 
39 	/* don't exceed max specified SPI CLK frequency */
40 	if (spi->max_speed_hz > MAX_SPI_FREQ_HZ) {
41 		dev_err(&spi->dev, "SPI CLK %d Hz?\n", spi->max_speed_hz);
42 		return -EINVAL;
43 	}
44 
45 	spi->bits_per_word = 16;
46 	err = spi_setup(spi);
47 	if (err) {
48 		dev_dbg(&spi->dev, "spi master doesn't support 16 bits/word\n");
49 		return err;
50 	}
51 
52 	regmap = devm_regmap_init_spi(spi, &ad7879_spi_regmap_config);
53 	if (IS_ERR(regmap))
54 		return PTR_ERR(regmap);
55 
56 	ts = ad7879_probe(&spi->dev, regmap, spi->irq, BUS_SPI, AD7879_DEVID);
57 	if (IS_ERR(ts))
58 		return PTR_ERR(ts);
59 
60 	spi_set_drvdata(spi, ts);
61 
62 	return 0;
63 }
64 
65 static int ad7879_spi_remove(struct spi_device *spi)
66 {
67 	struct ad7879 *ts = spi_get_drvdata(spi);
68 
69 	ad7879_remove(ts);
70 
71 	return 0;
72 }
73 
74 #ifdef CONFIG_OF
75 static const struct of_device_id ad7879_spi_dt_ids[] = {
76 	{ .compatible = "adi,ad7879", },
77 	{ }
78 };
79 MODULE_DEVICE_TABLE(of, ad7879_spi_dt_ids);
80 #endif
81 
82 static struct spi_driver ad7879_spi_driver = {
83 	.driver = {
84 		.name	= "ad7879",
85 		.pm	= &ad7879_pm_ops,
86 		.of_match_table = of_match_ptr(ad7879_spi_dt_ids),
87 	},
88 	.probe		= ad7879_spi_probe,
89 	.remove		= ad7879_spi_remove,
90 };
91 
92 module_spi_driver(ad7879_spi_driver);
93 
94 MODULE_AUTHOR("Michael Hennerich <hennerich@blackfin.uclinux.org>");
95 MODULE_DESCRIPTION("AD7879(-1) touchscreen SPI bus driver");
96 MODULE_LICENSE("GPL");
97 MODULE_ALIAS("spi:ad7879");
98