aboutsummaryrefslogtreecommitdiffstats
path: root/recipes/linux/linux-2.6.34/ts72xx/0018-ts72xx_spi_tmp124.patch
blob: cbc5a659654e6a202d6bbe2a76a6bb4f336b7324 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
From 314f5d55f44d63c4ff418260580b93727eee00dc Mon Sep 17 00:00:00 2001
From: Matthieu Crapet <mcrapet@gmail.com>
Date: Tue, 22 Jun 2010 15:48:27 +0200
Subject: [PATCH 18/18] ts72xx_spi_tmp124

It's an option. A 3-wire spi temperature sensor can be populated on TS-72XX sbc.
---
 arch/arm/mach-ep93xx/ts72xx.c |   61 ++++++++++++++++
 drivers/spi/Kconfig           |    7 ++
 drivers/spi/Makefile          |    1 +
 drivers/spi/tmp124.c          |  158 +++++++++++++++++++++++++++++++++++++++++
 4 files changed, 227 insertions(+), 0 deletions(-)
 create mode 100644 drivers/spi/tmp124.c

diff --git a/arch/arm/mach-ep93xx/ts72xx.c b/arch/arm/mach-ep93xx/ts72xx.c
index 475559e..bf3a666 100644
--- a/arch/arm/mach-ep93xx/ts72xx.c
+++ b/arch/arm/mach-ep93xx/ts72xx.c
@@ -23,7 +23,9 @@
 #include <linux/i2c-gpio.h>
 #include <linux/mtd/nand.h>
 #include <linux/mtd/partitions.h>
+#include <linux/spi/spi.h>
 
+#include <mach/ep93xx_spi.h>
 #include <mach/hardware.h>
 #include <mach/ts72xx.h>
 
@@ -367,6 +369,60 @@ static struct i2c_gpio_platform_data ts72xx_i2c_gpio_data = {
 static struct i2c_board_info __initdata ts72xx_i2c_board_info[] = {
 };
 
+/*************************************************************************
+ * SPI
+ *************************************************************************/
+#if defined(CONFIG_SPI_TMP124) || defined(CONFIG_SPI_TMP124_MODULE)
+
+/* this is our GPIO line used for chip select */
+#define TMP124_CHIP_SELECT_GPIO EP93XX_GPIO_LINE_MCCD2
+
+static int ts72xx_tmp124_setup(struct spi_device *spi)
+{
+	int err;
+
+	err = gpio_request(TMP124_CHIP_SELECT_GPIO, spi->modalias);
+	if (err)
+		return err;
+
+	gpio_direction_output(TMP124_CHIP_SELECT_GPIO, 1);
+
+	return 0;
+}
+
+static void ts72xx_tmp124_cleanup(struct spi_device *spi)
+{
+	gpio_set_value(TMP124_CHIP_SELECT_GPIO, 1);
+	gpio_direction_input(TMP124_CHIP_SELECT_GPIO);
+	gpio_free(TMP124_CHIP_SELECT_GPIO);
+}
+
+static void ts72xx_tmp124_cs_control(struct spi_device *spi, int value)
+{
+	gpio_set_value(TMP124_CHIP_SELECT_GPIO, value);
+}
+
+static struct ep93xx_spi_chip_ops ts72xx_tmp124_ops = {
+	.setup		= ts72xx_tmp124_setup,
+	.cleanup	= ts72xx_tmp124_cleanup,
+	.cs_control	= ts72xx_tmp124_cs_control,
+};
+
+static struct spi_board_info ts72xx_spi_devices[] = {
+	{
+		.modalias		= "tmp124",
+		.max_speed_hz		= 2 * 1000 * 1000,
+		.bus_num		= 0,
+		.chip_select		= 0,
+		.controller_data	= &ts72xx_tmp124_ops,
+	},
+};
+
+static struct ep93xx_spi_info ts72xx_spi_info = {
+	.num_chipselect	= ARRAY_SIZE(ts72xx_spi_devices),
+};
+#endif
+
 static void __init ts72xx_init_machine(void)
 {
 	ep93xx_init_devices();
@@ -380,6 +436,11 @@ static void __init ts72xx_init_machine(void)
 			ts72xx_i2c_board_info,
 			ARRAY_SIZE(ts72xx_i2c_board_info));
 
+	#if defined(CONFIG_SPI_TMP124) || defined(CONFIG_SPI_TMP124_MODULE)
+	ep93xx_register_spi(&ts72xx_spi_info, ts72xx_spi_devices,
+			ARRAY_SIZE(ts72xx_spi_devices));
+	#endif
+
 	if (is_max197_installed()) {
 		platform_device_register(&ts72xx_max197_device);
 	}
diff --git a/drivers/spi/Kconfig b/drivers/spi/Kconfig
index 2b2f4c3..9f40a43 100644
--- a/drivers/spi/Kconfig
+++ b/drivers/spi/Kconfig
@@ -372,6 +372,13 @@ config SPI_TLE62X0
 	  sysfs interface, with each line presented as a kind of GPIO
 	  exposing both switch control and diagnostic feedback.
 
+config SPI_TMP124
+	tristate "Texas Instruments TMP1224, TMP124"
+	depends on SPI_MASTER && SYSFS
+	help
+	  SPI driver for TMP12X temperature sensor chips.
+	  This provides a sysfs entry for temperature reading (2�C accurate).
+
 #
 # Add new SPI protocol masters in alphabetical order above this line
 #
diff --git a/drivers/spi/Makefile b/drivers/spi/Makefile
index 377f845..2798b38 100644
--- a/drivers/spi/Makefile
+++ b/drivers/spi/Makefile
@@ -56,6 +56,7 @@ spi_s3c24xx_hw-$(CONFIG_SPI_S3C24XX_FIQ) += spi_s3c24xx_fiq.o
 # SPI protocol drivers (device/link on bus)
 obj-$(CONFIG_SPI_SPIDEV)	+= spidev.o
 obj-$(CONFIG_SPI_TLE62X0)	+= tle62x0.o
+obj-$(CONFIG_SPI_TMP124)	+= tmp124.o
 # 	... add above this line ...
 
 # SPI slave controller drivers (upstream link)
diff --git a/drivers/spi/tmp124.c b/drivers/spi/tmp124.c
new file mode 100644
index 0000000..e41ec8c
--- /dev/null
+++ b/drivers/spi/tmp124.c
@@ -0,0 +1,158 @@
+/*
+ *  TMP124 SPI protocol driver
+ *
+ * (c) Copyright 2008-2010  Matthieu Crapet <mcrapet@gmail.com>
+ * Based on tle62x0.c by Ben Dooks, <ben@simtec.co.uk>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * Note: The chip uses a '3-wire SPI' (miso and mosi are the same pin).
+ */
+
+#include <linux/module.h>
+#include <linux/kernel.h>
+#include <linux/init.h>
+#include <linux/spi/spi.h>
+#include <linux/slab.h>
+#include <linux/sched.h>
+
+struct tmp124_state {
+	struct spi_device *bus;
+	u8 tx_buff[2];
+	u8 rx_buff[2];
+};
+
+static inline int tmp124_write_then_read(struct tmp124_state *st)
+{
+	struct spi_message msg;
+	struct spi_transfer xfer[2] = {
+		{
+			.tx_buf		= st->tx_buff,
+			.rx_buf		= NULL,
+			.len		= 2,
+		}, {
+			.tx_buf		= NULL,
+			.rx_buf		= st->rx_buff,
+			.len		= 2,
+		}
+	};
+
+	spi_message_init(&msg);
+	spi_message_add_tail(&xfer[0], &msg);
+	spi_sync(st->bus, &msg);
+
+	/* SPI_3WIRE is not handled by ep93xx_spi, the 2 messages must be
+	 * splitted. We must wait to not confuse driver with read/write. */
+	schedule_timeout(usecs_to_jiffies(1000));
+
+	spi_message_init(&msg);
+	spi_message_add_tail(&xfer[1], &msg);
+	return spi_sync(st->bus, &msg);
+}
+
+static ssize_t tmp124_temperature_show(struct device *dev,
+		struct device_attribute *attr, char *buf)
+{
+	struct tmp124_state *st = dev_get_drvdata(dev);
+	int ret;
+
+	((short *)st->tx_buff)[0] = 0x8000;
+
+	ret = tmp124_write_then_read(st);
+	if (ret < 0) {
+		dev_err(&st->bus->dev, "tmp124_write_then_read\n");
+		ret = 0;
+	} else {
+		signed long val = ((short *)st->rx_buff)[0];
+
+		val = val >> 3;
+
+		/* 2 digit precision (0.0625*100) */
+		val = (val * 50) / 8;
+		ret = snprintf(buf, PAGE_SIZE, "%ld.%02ld\n", val/100, abs(val%100));
+	}
+	return ret;
+}
+
+static DEVICE_ATTR(temperature, S_IRUGO, tmp124_temperature_show, NULL);
+
+static int __devinit tmp124_probe(struct spi_device *spi)
+{
+	struct tmp124_state *st;
+	int ret;
+
+	st = kzalloc(sizeof(struct tmp124_state), GFP_KERNEL);
+	if (st == NULL) {
+		dev_err(&spi->dev, "no memory for device state\n");
+		return -ENOMEM;
+	}
+
+	/* required config */
+	spi->bits_per_word = 16;
+        spi->mode = SPI_MODE_0;
+
+	ret = spi_setup(spi);
+	if (ret) {
+		dev_err(&spi->dev, "setup device\n");
+		goto err;
+	}
+
+	ret = device_create_file(&spi->dev, &dev_attr_temperature);
+	if (ret) {
+		dev_err(&spi->dev, "cannot create temperature attribute\n");
+		goto err;
+	}
+
+	st->bus = spi;
+	spi_set_drvdata(spi, st);
+	return 0;
+
+err:
+	kfree(st);
+	return ret;
+}
+
+static int __devexit tmp124_remove(struct spi_device *spi)
+{
+	struct tmp124_state *st = spi_get_drvdata(spi);
+
+	device_remove_file(&spi->dev, &dev_attr_temperature);
+	kfree(st);
+	return 0;
+}
+
+static struct spi_driver tmp124_driver = {
+	.driver = {
+		.name	= "tmp124",
+		.owner	= THIS_MODULE,
+	},
+	.probe	= tmp124_probe,
+	.remove	= __devexit_p(tmp124_remove),
+};
+
+static __init int tmp124_init(void)
+{
+	return spi_register_driver(&tmp124_driver);
+}
+
+static __exit void tmp124_exit(void)
+{
+	spi_unregister_driver(&tmp124_driver);
+}
+
+module_init(tmp124_init);
+module_exit(tmp124_exit);
+
+MODULE_AUTHOR("Matthieu Crapet <mcrapet@gmail.com>");
+MODULE_DESCRIPTION("TMP124 SPI Protocol Driver");
+MODULE_LICENSE("GPL");
+MODULE_ALIAS("spi:tmp124");
+MODULE_VERSION("0.2");
-- 
1.7.1