OMAPDSS: VENC: fix NULL pointer dereference in DSS2 VENC sysfs debug attr on OMAP4
[zen-stable.git] / drivers / mfd / stmpe-spi.c
blobb58c43c7ea93bf8f913f62a0774eb109175ebc45
1 /*
2 * ST Microelectronics MFD: stmpe's spi client specific driver
4 * Copyright (C) ST Microelectronics SA 2011
6 * License Terms: GNU General Public License, version 2
7 * Author: Viresh Kumar <viresh.kumar@st.com> for ST Microelectronics
8 */
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"
17 #define READ_CMD (1 << 7)
19 static int spi_reg_read(struct stmpe *stmpe, u8 reg)
21 struct spi_device *spi = stmpe->client;
22 int status = spi_w8r16(spi, reg | READ_CMD);
24 return (status < 0) ? status : status >> 8;
27 static int spi_reg_write(struct stmpe *stmpe, u8 reg, u8 val)
29 struct spi_device *spi = stmpe->client;
30 u16 cmd = (val << 8) | reg;
32 return spi_write(spi, (const u8 *)&cmd, 2);
35 static int spi_block_read(struct stmpe *stmpe, u8 reg, u8 length, u8 *values)
37 int ret, i;
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;
46 return 0;
49 static int spi_block_write(struct stmpe *stmpe, u8 reg, u8 length,
50 const u8 *values)
52 int ret = 0, i;
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;
60 return ret;
63 static void spi_init(struct stmpe *stmpe)
65 struct spi_device *spi = stmpe->client;
67 spi->bits_per_word = 8;
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);
73 if (spi_setup(spi) < 0)
74 dev_dbg(&spi->dev, "spi_setup failed\n");
77 static 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,
85 static int __devinit
86 stmpe_spi_probe(struct spi_device *spi)
88 const struct spi_device_id *id = spi_get_device_id(spi);
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;
97 spi_ci.irq = spi->irq;
98 spi_ci.client = spi;
99 spi_ci.dev = &spi->dev;
101 return stmpe_probe(&spi_ci, id->driver_data);
104 static int __devexit stmpe_spi_remove(struct spi_device *spi)
106 struct stmpe *stmpe = dev_get_drvdata(&spi->dev);
108 return stmpe_remove(stmpe);
111 static const struct spi_device_id stmpe_spi_id[] = {
112 { "stmpe610", STMPE610 },
113 { "stmpe801", STMPE801 },
114 { "stmpe811", STMPE811 },
115 { "stmpe1601", STMPE1601 },
116 { "stmpe2401", STMPE2401 },
117 { "stmpe2403", STMPE2403 },
120 MODULE_DEVICE_TABLE(spi, stmpe_id);
122 static struct spi_driver stmpe_spi_driver = {
123 .driver = {
124 .name = "stmpe-spi",
125 .bus = &spi_bus_type,
126 .owner = THIS_MODULE,
127 #ifdef CONFIG_PM
128 .pm = &stmpe_dev_pm_ops,
129 #endif
131 .probe = stmpe_spi_probe,
132 .remove = __devexit_p(stmpe_spi_remove),
133 .id_table = stmpe_spi_id,
136 static int __init stmpe_init(void)
138 return spi_register_driver(&stmpe_spi_driver);
140 subsys_initcall(stmpe_init);
142 static void __exit stmpe_exit(void)
144 spi_unregister_driver(&stmpe_spi_driver);
146 module_exit(stmpe_exit);
148 MODULE_LICENSE("GPL v2");
149 MODULE_DESCRIPTION("STMPE MFD SPI Interface Driver");
150 MODULE_AUTHOR("Viresh Kumar <viresh.kumar@st.com>");