blob: 9edfe864cc056b5cd543fd696d63a325cda34274 [file] [log] [blame]
Viresh Kumare7899952011-11-17 11:02:21 +05301/*
2 * ST Microelectronics MFD: stmpe's spi client specific driver
3 *
4 * Copyright (C) ST Microelectronics SA 2011
5 *
6 * License Terms: GNU General Public License, version 2
Viresh Kumar10d89352012-06-20 12:53:02 -07007 * Author: Viresh Kumar <viresh.linux@gmail.com> for ST Microelectronics
Viresh Kumare7899952011-11-17 11:02:21 +05308 */
9
10#include <linux/spi/spi.h>
11#include <linux/interrupt.h>
12#include <linux/kernel.h>
13#include <linux/module.h>
14#include <linux/types.h>
15#include "stmpe.h"
16
17#define READ_CMD (1 << 7)
18
19static int spi_reg_read(struct stmpe *stmpe, u8 reg)
20{
21 struct spi_device *spi = stmpe->client;
22 int status = spi_w8r16(spi, reg | READ_CMD);
23
24 return (status < 0) ? status : status >> 8;
25}
26
27static int spi_reg_write(struct stmpe *stmpe, u8 reg, u8 val)
28{
29 struct spi_device *spi = stmpe->client;
30 u16 cmd = (val << 8) | reg;
31
32 return spi_write(spi, (const u8 *)&cmd, 2);
33}
34
35static int spi_block_read(struct stmpe *stmpe, u8 reg, u8 length, u8 *values)
36{
37 int ret, i;
38
39 for (i = 0; i < length; i++) {
40 ret = spi_reg_read(stmpe, reg + i);
41 if (ret < 0)
42 return ret;
43 *(values + i) = ret;
44 }
45
46 return 0;
47}
48
49static int spi_block_write(struct stmpe *stmpe, u8 reg, u8 length,
50 const u8 *values)
51{
52 int ret = 0, i;
53
54 for (i = length; i > 0; i--, reg++) {
55 ret = spi_reg_write(stmpe, reg, *(values + i - 1));
56 if (ret < 0)
57 return ret;
58 }
59
60 return ret;
61}
62
63static void spi_init(struct stmpe *stmpe)
64{
65 struct spi_device *spi = stmpe->client;
66
67 spi->bits_per_word = 8;
68
69 /* This register is only present for stmpe811 */
70 if (stmpe->variant->id_val == 0x0811)
71 spi_reg_write(stmpe, STMPE811_REG_SPI_CFG, spi->mode);
72
73 if (spi_setup(spi) < 0)
74 dev_dbg(&spi->dev, "spi_setup failed\n");
75}
76
77static struct stmpe_client_info spi_ci = {
78 .read_byte = spi_reg_read,
79 .write_byte = spi_reg_write,
80 .read_block = spi_block_read,
81 .write_block = spi_block_write,
82 .init = spi_init,
83};
84
85static int __devinit
86stmpe_spi_probe(struct spi_device *spi)
87{
88 const struct spi_device_id *id = spi_get_device_id(spi);
89
90 /* don't exceed max specified rate - 1MHz - Limitation of STMPE */
91 if (spi->max_speed_hz > 1000000) {
92 dev_dbg(&spi->dev, "f(sample) %d KHz?\n",
93 (spi->max_speed_hz/1000));
94 return -EINVAL;
95 }
96
97 spi_ci.irq = spi->irq;
98 spi_ci.client = spi;
99 spi_ci.dev = &spi->dev;
100
101 return stmpe_probe(&spi_ci, id->driver_data);
102}
103
104static int __devexit stmpe_spi_remove(struct spi_device *spi)
105{
106 struct stmpe *stmpe = dev_get_drvdata(&spi->dev);
107
108 return stmpe_remove(stmpe);
109}
110
111static const struct spi_device_id stmpe_spi_id[] = {
Viresh Kumar1cda2392011-11-17 11:02:22 +0530112 { "stmpe610", STMPE610 },
Viresh Kumar7f7f4ea2011-11-17 11:02:23 +0530113 { "stmpe801", STMPE801 },
Viresh Kumare7899952011-11-17 11:02:21 +0530114 { "stmpe811", STMPE811 },
115 { "stmpe1601", STMPE1601 },
116 { "stmpe2401", STMPE2401 },
117 { "stmpe2403", STMPE2403 },
118 { }
119};
120MODULE_DEVICE_TABLE(spi, stmpe_id);
121
122static struct spi_driver stmpe_spi_driver = {
123 .driver = {
124 .name = "stmpe-spi",
Viresh Kumare7899952011-11-17 11:02:21 +0530125 .owner = THIS_MODULE,
126#ifdef CONFIG_PM
127 .pm = &stmpe_dev_pm_ops,
128#endif
129 },
130 .probe = stmpe_spi_probe,
131 .remove = __devexit_p(stmpe_spi_remove),
132 .id_table = stmpe_spi_id,
133};
134
135static int __init stmpe_init(void)
136{
137 return spi_register_driver(&stmpe_spi_driver);
138}
139subsys_initcall(stmpe_init);
140
141static void __exit stmpe_exit(void)
142{
143 spi_unregister_driver(&stmpe_spi_driver);
144}
145module_exit(stmpe_exit);
146
147MODULE_LICENSE("GPL v2");
148MODULE_DESCRIPTION("STMPE MFD SPI Interface Driver");
Viresh Kumar10d89352012-06-20 12:53:02 -0700149MODULE_AUTHOR("Viresh Kumar <viresh.linux@gmail.com>");