]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/spi/tsc210x.c
1d2ac94084c477590d151a38fe96c3f2090ea56c
[linux-2.6-omap-h63xx.git] / drivers / spi / tsc210x.c
1 /*
2  * tsc210x.c - TSC2101/2102/... driver core
3  *
4  * Copyright (c) 2005-2007 Andrzej Zaborowski  <balrog@zabor.org>
5  *
6  * This package is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This package is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this package; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  */
20
21 #include <linux/module.h>
22 #include <linux/init.h>
23 #include <linux/errno.h>
24 #include <linux/platform_device.h>
25 #include <linux/suspend.h>
26 #include <linux/interrupt.h>
27 #include <linux/clk.h>
28 #include <linux/err.h>
29 #include <linux/mutex.h>
30 #include <linux/workqueue.h>
31 #include <linux/completion.h>
32 #include <linux/autoconf.h>
33 #include <linux/spi/spi.h>
34 #include <linux/spi/tsc210x.h>
35
36
37 /* NOTE:  It should be straightforward to make this driver framework handle
38  * tsc2100 and tsc2111 chips, and maybe others too.  The main differences
39  * are in the audio codec capabilities, but there are also some differences
40  * in how the various sensors (including touchscreen) are handled.
41  */
42
43 /* Bit field definitions for chip registers */
44
45 /* Scan X, Y, Z1, Z2, chip controlled, 12-bit, 16 samples, 500 usec */
46 #define TSC210X_ADC_TS_CONTROL          0x8bf4
47 /* Scan BAT1, BAT2, AUX1, AUX2, 12-bit, 16 samples, 500 usec */
48 #define TSC210X_ADC_SCAN_CONTROL        0x2ff4
49 /* Scan TEMP1, 12-bit, 16 samples, 500 usec */
50 #define TSC210X_ADC_T1_CONTROL          0x2bf4
51 /* Scan TEMP2, 12-bit, 16 samples, 500 usec */
52 #define TSC210X_ADC_T2_CONTROL          0x33f4
53 /* PINT/DAV acts as DAV */
54 #define TSC210X_ADC_DAV                 0x4000
55 /* Internal reference, 100 usec delay, 1.25 V reference */
56 #define TSC210X_ADC_INT_REF             0x0016
57 /* External reference, 100 usec delay, 1.25 V reference */
58 #define TSC210X_ADC_EXT_REF             0x0002
59 /* 84 usec precharge time, 32 usec sense time */
60 #define TSC210X_CONFIG_TIMES            0x0008
61 /* The reset sequence */
62 #define TSC210X_RESET                   0xbb00
63 /* Pen Status bit */
64 #define TSC210X_ADC_PSTCM               (1 << 15)
65 /* A/D Status bit */
66 #define TSC210X_ADC_ADST                (1 << 14)
67 /* (At least) One of X, Y, Z1, Z2 contains data */
68 #define TSC210X_TS_DAV                  0x0780
69 /* (At least) One of BAT1, BAT2, AUX1, AUX2 contains data */
70 #define TSC210X_PS_DAV                  0x0078
71 /* TEMP1 contains data */
72 #define TSC210X_T1_DAV                  0x0004
73 /* TEMP2 contains data */
74 #define TSC210X_T2_DAV                  0x0002
75 #define TSC2101_DAC_ON                  0x0000
76 #define TSC2101_DAC_OFF                 0xe7fc
77 #define TSC2102_DAC_ON                  0x3ba0
78 #define TSC2102_DAC_OFF                 0xafa0
79 #define TSC210X_FS44K                   (1 << 13)
80 #define TSC210X_PLL1_OFF                0x0000
81 #define TSC210X_PLL1_44K                0x811c
82 #define TSC210X_PLL1_48K                0x8120
83 #define TSC210X_PLL2_44K                (5462 << 2)
84 #define TSC210X_PLL2_48K                (1920 << 2)
85 #define TSC210X_SLVMS                   (1 << 11)
86 #define TSC210X_DEEMPF                  (1 << 0)
87 #define TSC2102_BASSBC                  (1 << 1)
88 #define TSC210X_KEYCLICK_OFF            0x0000
89
90 #define CS_CHANGE(val)                  0
91
92 struct tsc210x_spi_req {
93         struct spi_device *dev;
94         u16 command;
95         u16 data;
96         struct spi_message message;
97 };
98
99 struct tsc210x_dev {
100         enum tsc_type {
101                 tsc2101,
102                 tsc2102,
103         } kind;
104         struct tsc210x_config *pdata;
105         struct clk *bclk_ck;
106
107         struct workqueue_struct *queue;
108         struct delayed_work ts_worker;          /* Poll-wait for PEN UP */
109         struct delayed_work sensor_worker;      /* Scan the ADC inputs */
110         struct mutex queue_lock;
111         struct completion data_avail;
112
113         tsc210x_touch_t touch_cb;
114         void *touch_cb_ctx;
115
116         tsc210x_coords_t coords_cb;
117         void *coords_cb_ctx;
118
119         tsc210x_ports_t ports_cb;
120         void *ports_cb_ctx;
121
122         tsc210x_temp_t temp1_cb;
123         void *temp2_cb_ctx;
124
125         tsc210x_temp_t temp2_cb;
126         void *temp1_cb_ctx;
127
128         struct spi_device *spi;
129         struct spi_transfer *transfers;
130         struct tsc210x_spi_req req_adc;
131         struct tsc210x_spi_req req_status;
132         struct tsc210x_spi_req req_mode;
133         struct tsc210x_spi_req req_stop;
134
135         int pendown;
136         int flushing;                   /* Queue flush in progress */
137         u16 status;
138         u16 adc_data[4];
139         int bat[2], aux[2], temp[2];
140 };
141
142 static struct {
143         unsigned int ts_msecs;          /* Interval for .ts_timer */
144         unsigned int mode_msecs;        /* Interval for .mode_timer */
145 } settings;
146
147 module_param_named(touch_check_msecs, settings.ts_msecs, uint, 0);
148 MODULE_PARM_DESC(touch_check_msecs, "Pen-up polling interval in msecs");
149
150 module_param_named(sensor_scan_msecs, settings.mode_msecs, uint, 0);
151 MODULE_PARM_DESC(sensor_scan_msecs, "Temperature & battery scan interval");
152
153 int tsc210x_write_sync(struct tsc210x_dev *dev,
154                 int page, u8 address, u16 data)
155 {
156         static struct tsc210x_spi_req req;
157         static struct spi_transfer transfer[2];
158         int ret;
159
160         spi_message_init(&req.message);
161
162         /* Address */
163         req.command = (page << 11) | (address << 5);
164         transfer[0].tx_buf = &req.command;
165         transfer[0].rx_buf = NULL;
166         transfer[0].len = 2;
167         spi_message_add_tail(&transfer[0], &req.message);
168
169         /* Data */
170         transfer[1].tx_buf = &data;
171         transfer[1].rx_buf = NULL;
172         transfer[1].len = 2;
173         transfer[1].cs_change = CS_CHANGE(1);
174         spi_message_add_tail(&transfer[1], &req.message);
175
176         ret = spi_sync(dev->spi, &req.message);
177         if (!ret && req.message.status)
178                 ret = req.message.status;
179         if (ret)
180                 dev_dbg(&dev->spi->dev, "write_sync --> %d\n", ret);
181
182         return ret;
183 }
184 EXPORT_SYMBOL(tsc210x_write_sync);
185
186 int tsc210x_reads_sync(struct tsc210x_dev *dev,
187                 int page, u8 startaddress, u16 *data, int numregs)
188 {
189         static struct tsc210x_spi_req req;
190         static struct spi_transfer transfer[6];
191         int ret, i, j;
192
193         if (numregs + 1 > ARRAY_SIZE(transfer))
194                 return -EINVAL;
195
196         spi_message_init(&req.message);
197         i = 0;
198         j = 0;
199
200         /* Address */
201         req.command = 0x8000 | (page << 11) | (startaddress << 5);
202         transfer[i].tx_buf = &req.command;
203         transfer[i].rx_buf = NULL;
204         transfer[i].len = 2;
205         spi_message_add_tail(&transfer[i ++], &req.message);
206
207         /* Data */
208         while (j < numregs) {
209                 transfer[i].tx_buf = NULL;
210                 transfer[i].rx_buf = &data[j ++];
211                 transfer[i].len = 2;
212                 transfer[i].cs_change = CS_CHANGE(j == numregs);
213                 spi_message_add_tail(&transfer[i ++], &req.message);
214         }
215
216         ret = spi_sync(dev->spi, &req.message);
217         if (!ret && req.message.status)
218                 ret = req.message.status;
219         if (ret)
220                 dev_dbg(&dev->spi->dev, "reads_sync --> %d\n", ret);
221
222         return ret;
223 }
224 EXPORT_SYMBOL(tsc210x_reads_sync);
225
226 int tsc210x_read_sync(struct tsc210x_dev *dev, int page, u8 address)
227 {
228         u16 ret;
229         int status;
230
231         status = tsc210x_reads_sync(dev, page, address, &ret, 1);
232         return status ? : ret;
233 }
234 EXPORT_SYMBOL(tsc210x_read_sync);
235
236
237 static void tsc210x_submit_async(struct tsc210x_spi_req *spi)
238 {
239         int ret;
240
241         ret = spi_async(spi->dev, &spi->message);
242         if (ret)
243                 dev_dbg(&spi->dev->dev, "%s: error %i in SPI request\n",
244                                 __FUNCTION__, ret);
245 }
246
247 static void tsc210x_request_alloc(struct tsc210x_dev *dev,
248                 struct tsc210x_spi_req *spi, int direction,
249                 int page, u8 startaddress, int numregs, u16 *data,
250                 void (*complete)(struct tsc210x_dev *context),
251                 struct spi_transfer **transfer)
252 {
253         spi->dev = dev->spi;
254
255         if (direction == 1)     /* Write */
256                 numregs = 2;
257         else                    /* Read */
258                 numregs += 1;
259
260         spi_message_init(&spi->message);
261         spi->message.complete = (void (*)(void *)) complete;
262         spi->message.context = dev;
263
264         /* Address */
265         spi->command = (page << 11) | (startaddress << 5);
266         if (direction != 1)
267                 spi->command |= 1 << 15;
268
269         (*transfer)->tx_buf = &spi->command;
270         (*transfer)->rx_buf = NULL;
271         (*transfer)->len = 2;
272         spi_message_add_tail((*transfer) ++, &spi->message);
273
274         /* Data */
275         while (-- numregs) {
276                 if (direction == 1)
277                         (*transfer)->tx_buf = &spi->data;
278                 else
279                         (*transfer)->rx_buf = data++;
280                 (*transfer)->len = 2;
281                 (*transfer)->cs_change = CS_CHANGE(numregs != 1);
282                 spi_message_add_tail((*transfer) ++, &spi->message);
283         }
284 }
285
286 #define tsc210x_cb_register_func(cb, cb_t)      \
287 int tsc210x_ ## cb(struct device *dev, cb_t handler, void *context)     \
288 {       \
289         struct tsc210x_dev *tsc = dev_get_drvdata(dev); \
290         \
291         /* Lock the module */   \
292         if (handler && !tsc->cb)        \
293                 if (!try_module_get(THIS_MODULE)) {     \
294                         dev_err(dev, "Failed to get TSC module\n");     \
295                 }       \
296         if (!handler && tsc->cb)        \
297                 module_put(THIS_MODULE);        \
298         \
299         tsc->cb = handler;      \
300         tsc->cb ## _ctx = context;      \
301         return 0;       \
302 } \
303 EXPORT_SYMBOL(tsc210x_ ## cb);
304
305 tsc210x_cb_register_func(touch_cb, tsc210x_touch_t)
306 tsc210x_cb_register_func(coords_cb, tsc210x_coords_t)
307 tsc210x_cb_register_func(ports_cb, tsc210x_ports_t)
308 tsc210x_cb_register_func(temp1_cb, tsc210x_temp_t)
309 tsc210x_cb_register_func(temp2_cb, tsc210x_temp_t)
310
311 #ifdef DEBUG
312 static void tsc210x_print_dav(struct tsc210x_dev *dev)
313 {
314         int status = tsc210x_read_sync(dev, TSC210X_TS_STATUS_CTRL);
315
316         if (status < 0) {
317                 dev_dbg(&dev->spi->dev, "status %d\n", status);
318                 return;
319         }
320
321         if (!(status & 0x0fff))
322                 return;
323
324         dev_dbg(&dev->spi->dev, "data in %s%s%s%s%s%s%s%s%s%s%s\n",
325                 (status & 0x0400) ? " X" : "",
326                 (status & 0x0200) ? " Y" : "",
327                 (status & 0x0100) ? " Z1" : "",
328                 (status & 0x0080) ? " Z2" : "",
329                 (status & 0x0040) ? " BAT1" : "",
330                 (status & 0x0020) ? " BAT2" : "",
331                 (status & 0x0010) ? " AUX1" : "",
332                 (status & 0x0008) ? " AUX2" : "",
333                 (status & 0x0004) ? " TEMP1" : "",
334                 (status & 0x0002) ? " TEMP2" : "",
335                 (status & 0x0001) ? " KP" : "");
336 }
337 #endif
338
339 static void tsc210x_complete_dummy(struct tsc210x_dev *dev)
340 {
341 }
342
343 static inline void tsc210x_touchscreen_mode(struct tsc210x_dev *dev)
344 {
345         /* Scan X, Y, Z1, Z2, chip controlled, 12-bit, 16 samples, 500 usec */
346         dev->req_mode.data = TSC210X_ADC_TS_CONTROL;
347         tsc210x_submit_async(&dev->req_mode);
348 }
349
350 static inline void tsc210x_portscan_mode(struct tsc210x_dev *dev)
351 {
352         /* Scan BAT1, BAT2, AUX1, AUX2, 12-bit, 16 samples, 500 usec */
353         dev->req_mode.data = TSC210X_ADC_SCAN_CONTROL;
354         tsc210x_submit_async(&dev->req_mode);
355 }
356
357 static inline void tsc210x_temp1_mode(struct tsc210x_dev *dev)
358 {
359         /* Scan TEMP1, 12-bit, 16 samples, 500 usec */
360         dev->req_mode.data = TSC210X_ADC_T1_CONTROL;
361         tsc210x_submit_async(&dev->req_mode);
362 }
363
364 static inline void tsc210x_temp2_mode(struct tsc210x_dev *dev)
365 {
366         /* Scan TEMP2, 12-bit, 16 samples, 500 usec */
367         dev->req_mode.data = TSC210X_ADC_T2_CONTROL;
368         tsc210x_submit_async(&dev->req_mode);
369 }
370
371 /* Abort current conversion if any */
372 static void tsc210x_new_mode(struct tsc210x_dev *dev)
373 {
374         dev->req_stop.data = TSC210X_ADC_ADST;
375         tsc210x_submit_async(&dev->req_stop);
376 }
377
378 static void tsc210x_queue_scan(struct tsc210x_dev *dev)
379 {
380         if (dev->pdata->monitor)
381                 if (!queue_delayed_work(dev->queue,
382                                 &dev->sensor_worker,
383                                 msecs_to_jiffies(settings.mode_msecs)))
384                         dev_err(&dev->spi->dev,
385                                         "%s: can't queue measurements\n",
386                                         __FUNCTION__);
387 }
388
389 static void tsc210x_queue_penup(struct tsc210x_dev *dev)
390 {
391         if (!queue_delayed_work(dev->queue,
392                         &dev->ts_worker,
393                         msecs_to_jiffies(settings.ts_msecs)))
394                 dev_err(&dev->spi->dev,
395                                 "%s: can't queue pen-up poll\n",
396                                 __FUNCTION__);
397 }
398
399 static void tsc210x_status_report(struct tsc210x_dev *dev)
400 {
401         /*
402          * Read all converted data from corresponding registers
403          * so that the ADC can move on to a new conversion.
404          */
405         if (dev->status & TSC210X_TS_DAV) {
406                 if (!dev->pendown && !dev->flushing) {
407                         dev->pendown = 1;
408                         if (dev->touch_cb)
409                                 dev->touch_cb(dev->touch_cb_ctx, 1);
410
411                         tsc210x_queue_penup(dev);
412                 }
413
414                 tsc210x_submit_async(&dev->req_adc);
415         }
416
417         if (dev->status & (TSC210X_PS_DAV | TSC210X_T1_DAV | TSC210X_T2_DAV))
418                 complete(&dev->data_avail);
419 }
420
421 static void tsc210x_data_report(struct tsc210x_dev *dev)
422 {
423         u16 adc_data[4];
424
425         if (dev->status & TSC210X_PS_DAV) {
426                 tsc210x_reads_sync(dev, TSC210X_TS_BAT1, adc_data, 4);
427                 /* NOTE: reads_sync() could fail */
428
429                 dev->bat[0] = adc_data[0];
430                 dev->bat[1] = adc_data[1];
431                 dev->aux[0] = adc_data[2];
432                 dev->aux[1] = adc_data[3];
433                 if (dev->ports_cb)
434                         dev->ports_cb(dev->ports_cb_ctx, dev->bat, dev->aux);
435         }
436
437         if (dev->status & TSC210X_T1_DAV) {
438                 dev->temp[0] = tsc210x_read_sync(dev, TSC210X_TS_TEMP1);
439
440                 if (dev->temp[0] >= 0 && dev->temp1_cb)
441                         dev->temp1_cb(dev->temp1_cb_ctx, dev->temp[0]);
442         }
443
444         if (dev->status & TSC210X_T2_DAV) {
445                 dev->temp[1] = tsc210x_read_sync(dev, TSC210X_TS_TEMP2);
446
447                 if (dev->temp[1] >= 0 && dev->temp2_cb)
448                         dev->temp2_cb(dev->temp2_cb_ctx, dev->temp[1]);
449         }
450 }
451
452 static void tsc210x_coords_report(struct tsc210x_dev *dev)
453 {
454         if (dev->coords_cb)
455                 dev->coords_cb(dev->coords_cb_ctx,
456                                 dev->adc_data[0], dev->adc_data[1],
457                                 dev->adc_data[2], dev->adc_data[3]);
458 }
459
460 /*
461  * There are at least three ways to check for pen-up:
462  *      - the PINT/DAV pin state,
463  *      - reading PSTCM bit in ADC Control register (D15, offset 0x00),
464  *      - reading ADST bit in ADC Control register (D14, offset 0x00),
465  *              ADC idle would indicate no screen touch.
466  * Unfortunately none of them seems to be 100% accurate and you will
467  * find they are totally inconsistent, i.e. you get to see any arbitrary
468  * combination of values in these three bits.  So we will busy-wait
469  * for a moment when the latter two indicate a pen-up, using a queue,
470  * before we report a pen-up.
471  */
472 static void tsc210x_pressure(struct work_struct *work)
473 {
474         struct tsc210x_dev *dev =
475                 container_of(work, struct tsc210x_dev, ts_worker.work);
476         int adc_status;
477
478         WARN_ON(!dev->pendown);
479
480         adc_status = tsc210x_read_sync(dev, TSC210X_TS_ADC_CTRL);
481         if (adc_status < 0) {
482                 dev_dbg(&dev->spi->dev, "pressure, err %d\n", adc_status);
483                 return;
484         }
485
486         if ((adc_status & TSC210X_ADC_PSTCM) != 0
487                         || !(adc_status & TSC210X_ADC_ADST))
488                 tsc210x_queue_penup(dev);
489         else {
490                 dev->pendown = 0;
491                 if (dev->touch_cb)
492                         dev->touch_cb(dev->touch_cb_ctx, 0);
493         }
494 }
495
496 static void tsc210x_wait_data(struct tsc210x_dev *dev)
497 {
498         wait_for_completion(&dev->data_avail);
499
500         tsc210x_data_report(dev);
501 }
502
503 static void tsc210x_input_scan(struct work_struct *work)
504 {
505         struct tsc210x_dev *dev = (struct tsc210x_dev *)
506                 container_of(work, struct tsc210x_dev, sensor_worker.work);
507
508         tsc210x_new_mode(dev);
509
510         if (dev->pdata->monitor &
511                         (TSC_BAT1 | TSC_BAT2 | TSC_AUX1 | TSC_AUX2)) {
512                 tsc210x_portscan_mode(dev);
513                 tsc210x_wait_data(dev);
514         }
515
516         if (dev->pdata->monitor & TSC_TEMP) {
517                 tsc210x_temp1_mode(dev);
518                 tsc210x_wait_data(dev);
519
520                 tsc210x_temp2_mode(dev);
521                 tsc210x_wait_data(dev);
522         }
523
524         tsc210x_touchscreen_mode(dev);
525
526         mutex_lock(&dev->queue_lock);
527         if (!dev->flushing)
528                 tsc210x_queue_scan(dev);
529         mutex_unlock(&dev->queue_lock);
530 }
531
532 /* ADC has finished a new conversion for us.  */
533 static irqreturn_t tsc210x_handler(int irq, void *dev_id)
534 {
535         struct tsc210x_dev *dev = (struct tsc210x_dev *) dev_id;
536
537         /* See what data became available.  */
538         tsc210x_submit_async(&dev->req_status);
539
540         return IRQ_HANDLED;
541 }
542
543 #if defined(CONFIG_SOUND) || defined(CONFIG_SOUND_MODULE)
544
545 /*
546  * FIXME the audio support shouldn't be included in upstream patches
547  * until it's ready.  They might be better as utility functions linked
548  * with a chip-specific tsc21xx audio module ... e.g. chips with input
549  * channels need more, as will ones with multiple output channels and
550  * so on.  Each of these functions should probably return a fault code,
551  * and will need to be exported so the sound drier can be modular.
552  */
553
554 /*
555  * Volume level values should be in the range [0, 127].
556  * Higher values mean lower volume.
557  */
558 void tsc210x_set_dac_volume(struct device *dev, u8 left_ch, u8 right_ch)
559 {
560         struct tsc210x_dev *tsc = dev_get_drvdata(dev);
561         int val;
562
563         if (tsc->kind == tsc2102) {
564                 /* All 0's or all 1's */
565                 if (left_ch == 0x00 || left_ch == 0x7f)
566                         left_ch ^= 0x7f;
567                 if (right_ch == 0x00 || right_ch == 0x7f)
568                         right_ch ^= 0x7f;
569         }
570
571         val = tsc210x_read_sync(tsc, TSC210X_DAC_GAIN_CTRL);
572         if (val < 0) {
573                 dev_dbg(dev, "%s, err %d\n", __FUNCTION__, val);
574                 return;
575         }
576
577         val &= 0x8080;  /* Preserve mute-bits */
578         val |= (left_ch << 8) | right_ch;
579
580         tsc210x_write_sync(tsc, TSC210X_DAC_GAIN_CTRL, val);
581         /* NOTE: write_sync() could fail */
582 }
583
584 void tsc210x_set_dac_mute(struct device *dev, int left_ch, int right_ch)
585 {
586         struct tsc210x_dev *tsc = dev_get_drvdata(dev);
587         int val;
588
589         val = tsc210x_read_sync(tsc, TSC210X_DAC_GAIN_CTRL);
590         if (val < 0) {
591                 dev_dbg(dev, "%s, err %d\n", __FUNCTION__, val);
592                 return;
593         }
594
595         val &= 0x7f7f;  /* Preserve volume settings */
596         val |= (left_ch << 15) | (right_ch << 7);
597
598         tsc210x_write_sync(tsc, TSC210X_DAC_GAIN_CTRL, val);
599         /* NOTE: write_sync() could fail */
600 }
601
602 void tsc210x_get_dac_mute(struct device *dev, int *left_ch, int *right_ch)
603 {
604         struct tsc210x_dev *tsc = dev_get_drvdata(dev);
605         int val;
606
607         val = tsc210x_read_sync(tsc, TSC210X_DAC_GAIN_CTRL);
608         if (val < 0) {
609                 dev_dbg(dev, "%s, err %d\n", __FUNCTION__, val);
610                 return;
611         }
612
613         *left_ch = !!(val & (1 << 15));
614         *right_ch = !!(val & (1 << 7));
615 }
616
617 void tsc210x_set_deemphasis(struct device *dev, int enable)
618 {
619         struct tsc210x_dev *tsc = dev_get_drvdata(dev);
620         int val;
621
622         val = tsc210x_read_sync(tsc, TSC210X_POWER_CTRL);
623         if (val < 0) {
624                 dev_dbg(dev, "%s, err %d\n", __FUNCTION__, val);
625                 return;
626         }
627
628         if (enable)
629                 val &= ~TSC210X_DEEMPF;
630         else
631                 val |= TSC210X_DEEMPF;
632
633         tsc210x_write_sync(tsc, TSC210X_POWER_CTRL, val);
634         /* NOTE: write_sync() could fail */
635 }
636
637 void tsc2102_set_bassboost(struct device *dev, int enable)
638 {
639         struct tsc210x_dev *tsc = dev_get_drvdata(dev);
640         int val;
641
642         val = tsc210x_read_sync(tsc, TSC210X_POWER_CTRL);
643         if (val < 0) {
644                 dev_dbg(dev, "%s, err %d\n", __FUNCTION__, val);
645                 return;
646         }
647
648         if (enable)
649                 val &= ~TSC2102_BASSBC;
650         else
651                 val |= TSC2102_BASSBC;
652
653         tsc210x_write_sync(tsc, TSC210X_POWER_CTRL, val);
654         /* NOTE: write_sync() could fail */
655 }
656
657 /*      {rate, dsor, fsref}     */
658 static const struct tsc210x_rate_info_s tsc2101_rates[] = {
659         /* Fsref / 6.0 */
660         {7350,  7,      1},
661         {8000,  7,      0},
662         /* Fsref / 5.5 */
663         {8018,  6,      1},
664         {8727,  6,      0},
665         /* Fsref / 5.0 */
666         {8820,  5,      1},
667         {9600,  5,      0},
668         /* Fsref / 4.0 */
669         {11025, 4,      1},
670         {12000, 4,      0},
671         /* Fsref / 3.0 */
672         {14700, 3,      1},
673         {16000, 3,      0},
674         /* Fsref / 2.0 */
675         {22050, 2,      1},
676         {24000, 2,      0},
677         /* Fsref / 1.5 */
678         {29400, 1,      1},
679         {32000, 1,      0},
680         /* Fsref */
681         {44100, 0,      1},
682         {48000, 0,      0},
683
684         {0,     0,      0},
685 };
686
687 /*      {rate, dsor, fsref}     */
688 static const struct tsc210x_rate_info_s tsc2102_rates[] = {
689         /* Fsref / 6.0 */
690         {7350,  63,     1},
691         {8000,  63,     0},
692         /* Fsref / 6.0 */
693         {7350,  54,     1},
694         {8000,  54,     0},
695         /* Fsref / 5.0 */
696         {8820,  45,     1},
697         {9600,  45,     0},
698         /* Fsref / 4.0 */
699         {11025, 36,     1},
700         {12000, 36,     0},
701         /* Fsref / 3.0 */
702         {14700, 27,     1},
703         {16000, 27,     0},
704         /* Fsref / 2.0 */
705         {22050, 18,     1},
706         {24000, 18,     0},
707         /* Fsref / 1.5 */
708         {29400, 9,      1},
709         {32000, 9,      0},
710         /* Fsref */
711         {44100, 0,      1},
712         {48000, 0,      0},
713
714         {0,     0,      0},
715 };
716
717 int tsc210x_set_rate(struct device *dev, int rate)
718 {
719         struct tsc210x_dev *tsc = dev_get_drvdata(dev);
720         int i;
721         int val;
722         const struct tsc210x_rate_info_s *rates;
723
724         if (tsc->kind == tsc2101)
725                 rates = tsc2101_rates;
726         else
727                 rates = tsc2102_rates;
728
729         for (i = 0; rates[i].sample_rate; i ++)
730                 if (rates[i].sample_rate == rate)
731                         break;
732         if (rates[i].sample_rate == 0) {
733                 dev_err(dev, "Unknown sampling rate %i.0 Hz\n", rate);
734                 return -EINVAL;
735         }
736
737         if (tsc->kind == tsc2101) {
738                 val = tsc210x_read_sync(tsc, TSC210X_AUDIO1_CTRL);
739                 if (val < 0) {
740                         dev_dbg(dev, "%s, err %d\n", __FUNCTION__, val);
741                         return val;
742                 }
743                 val &= ~((7 << 3) | (7 << 0));
744                 val |= rates[i].divisor << 3;
745                 val |= rates[i].divisor << 0;
746         } else
747                 val = rates[i].divisor;
748
749         tsc210x_write_sync(tsc, TSC210X_AUDIO1_CTRL, val);
750         /* NOTE: write_sync() could fail */
751
752         val = tsc210x_read_sync(tsc, TSC210X_AUDIO3_CTRL);
753         if (val < 0) {
754                 dev_dbg(dev, "%s, err %d\n", __FUNCTION__, val);
755                 return val;
756         }
757
758         if (tsc2102_rates[i].fs_44k) {
759                 tsc210x_write_sync(tsc,
760                                 TSC210X_AUDIO3_CTRL, val | TSC210X_FS44K);
761                 /* Enable Phase-locked-loop, set up clock dividers */
762                 tsc210x_write_sync(tsc, TSC210X_PLL1_CTRL, TSC210X_PLL1_44K);
763                 tsc210x_write_sync(tsc, TSC210X_PLL2_CTRL, TSC210X_PLL2_44K);
764         } else {
765                 tsc210x_write_sync(tsc,
766                                 TSC210X_AUDIO3_CTRL, val & ~TSC210X_FS44K);
767                 /* Enable Phase-locked-loop, set up clock dividers */
768                 tsc210x_write_sync(tsc, TSC210X_PLL1_CTRL, TSC210X_PLL1_48K);
769                 tsc210x_write_sync(tsc, TSC210X_PLL2_CTRL, TSC210X_PLL2_48K);
770         }
771
772         return 0;
773 }
774
775 /*
776  * Perform basic set-up with default values and power the DAC/ADC on.
777  */
778 void tsc210x_dac_power(struct device *dev, int on)
779 {
780         struct tsc210x_dev *tsc = dev_get_drvdata(dev);
781
782         /* NOTE: write_sync() could fail */
783         if (on) {
784                 /* 16-bit words, DSP mode, sample at Fsref */
785                 tsc210x_write_sync(tsc,
786                                 TSC210X_AUDIO1_CTRL, 0x0100);
787                 /* Keyclicks off, soft-stepping at normal rate */
788                 tsc210x_write_sync(tsc,
789                                 TSC210X_AUDIO2_CTRL, TSC210X_KEYCLICK_OFF);
790                 /* 44.1 kHz Fsref, continuous transfer mode, master DAC */
791                 tsc210x_write_sync(tsc,
792                                 TSC210X_AUDIO3_CTRL, 0x2000);
793                 /* Soft-stepping enabled, 1 dB MIX AGC hysteresis */
794                 tsc210x_write_sync(tsc,
795                                 TSC210X_AUDIO4_CTRL, 0x0000);
796
797                 /* PLL generates 44.1 kHz */
798                 tsc210x_write_sync(tsc,
799                                 TSC210X_PLL1_CTRL, TSC210X_PLL1_44K);
800                 tsc210x_write_sync(tsc,
801                                 TSC210X_PLL2_CTRL, TSC210X_PLL2_44K);
802
803                 /* Codec & DAC power up, virtual ground disabled */
804                 tsc210x_write_sync(tsc,
805                                 TSC210X_POWER_CTRL, (tsc->kind == tsc2101) ?
806                                 TSC2101_DAC_ON : TSC2102_DAC_ON);
807         } else {
808                 /* All off */
809                 tsc210x_write_sync(tsc,
810                                 TSC210X_AUDIO2_CTRL, TSC210X_KEYCLICK_OFF);
811                 tsc210x_write_sync(tsc,
812                                 TSC210X_PLL1_CTRL, TSC210X_PLL1_OFF);
813 #if 0
814                 tsc210x_write_sync(tsc,
815                                 TSC210X_POWER_CTRL, (tsc->kind == tsc2101) ?
816                                 TSC2102_DAC_OFF : TSC2102_DAC_OFF);
817 #endif
818         }
819 }
820
821 void tsc210x_set_i2s_master(struct device *dev, int state)
822 {
823         struct tsc210x_dev *tsc = dev_get_drvdata(dev);
824         int val;
825
826         val = tsc210x_read_sync(tsc, TSC210X_AUDIO3_CTRL);
827         if (val < 0) {
828                 dev_dbg(dev, "%s, err %d\n", __FUNCTION__, val);
829                 return;
830         }
831
832         /* NOTE: write_sync() could fail */
833         if (state)
834                 tsc210x_write_sync(tsc, TSC210X_AUDIO3_CTRL,
835                                 val | TSC210X_SLVMS);
836         else
837                 tsc210x_write_sync(tsc, TSC210X_AUDIO3_CTRL,
838                                 val & ~TSC210X_SLVMS);
839 }
840 #endif  /* CONFIG_SOUND */
841
842 static int tsc210x_configure(struct tsc210x_dev *dev)
843 {
844         /* NOTE: write_sync() could fail */
845
846         /* Reset the chip */
847         tsc210x_write_sync(dev, TSC210X_TS_RESET_CTRL, TSC210X_RESET);
848
849         /* Reference mode */
850         if (dev->pdata->use_internal)
851                 tsc210x_write_sync(dev,
852                                 TSC210X_TS_REF_CTRL, TSC210X_ADC_INT_REF);
853         else
854                 tsc210x_write_sync(dev,
855                                 TSC210X_TS_REF_CTRL, TSC210X_ADC_EXT_REF);
856
857         /* Precharge and sense delays, pen touch detection on */
858         tsc210x_write_sync(dev, TSC210X_TS_CONFIG_CTRL, TSC210X_CONFIG_TIMES);
859
860         /* PINT/DAV acts as DAV */
861         tsc210x_write_sync(dev, TSC210X_TS_STATUS_CTRL, TSC210X_ADC_DAV);
862
863         tsc210x_queue_scan(dev);
864         return 0;
865 }
866
867 void tsc210x_keyclick(struct tsc210x_dev *dev,
868                 int amplitude, int freq, int length)
869 {
870         int val;
871
872         val = tsc210x_read_sync(dev, TSC210X_AUDIO2_CTRL);
873         if (val < 0) {
874                 dev_dbg(&dev->spi->dev, "%s, err %d\n",
875                                 __FUNCTION__, val);
876                 return;
877         }
878         val &= 0x800f;
879
880         /* Set amplitude */
881         switch (amplitude) {
882         case 1:
883                 val |= 4 << 12;
884                 break;
885         case 2:
886                 val |= 7 << 12;
887                 break;
888         default:
889                 break;
890         }
891
892         /* Frequency */
893         val |= (freq & 0x7) << 8;
894
895         /* Round to nearest supported length */
896         if (dev->kind == tsc2101)
897                 val = (min(length - 1, 31) >> 1) << 4;
898         else {
899                 if (length > 20)
900                         val |= 4 << 4;
901                 else if (length > 6)
902                         val |= 3 << 4;
903                 else if (length > 4)
904                         val |= 2 << 4;
905                 else if (length > 2)
906                         val |= 1 << 4;
907         }
908
909         /* Enable keyclick */
910         val |= 0x8000;
911
912         /* NOTE: write_sync() could fail */
913         tsc210x_write_sync(dev, TSC210X_AUDIO2_CTRL, val);
914 }
915 EXPORT_SYMBOL(tsc210x_keyclick);
916
917 #ifdef CONFIG_PM
918 /*
919  * Suspend the chip.
920  */
921 static int
922 tsc210x_suspend(struct spi_device *spi, pm_message_t state)
923 {
924         struct tsc210x_dev *dev = dev_get_drvdata(&spi->dev);
925
926         if (!dev)
927                 return -ENODEV;
928
929         /* Stop the inputs scan loop */
930         mutex_lock(&dev->queue_lock);
931         dev->flushing = 1;
932         cancel_delayed_work(&dev->sensor_worker);
933         mutex_unlock(&dev->queue_lock);
934         flush_workqueue(dev->queue);
935
936         /* Wait until pen-up happens */
937         while (dev->pendown)
938                 flush_workqueue(dev->queue);
939
940         /* Abort current conversion and power down the ADC */
941         tsc210x_write_sync(dev, TSC210X_TS_ADC_CTRL, TSC210X_ADC_ADST);
942         /* NOTE: write_sync() could fail */
943
944         return 0;
945 }
946
947 /*
948  * Resume chip operation.
949  */
950 static int tsc210x_resume(struct spi_device *spi)
951 {
952         struct tsc210x_dev *dev = dev_get_drvdata(&spi->dev);
953         int err;
954
955         if (!dev)
956                 return 0;
957
958         mutex_lock(&dev->queue_lock);
959         err = tsc210x_configure(dev);
960
961         dev->flushing = 0;
962         mutex_unlock(&dev->queue_lock);
963
964         return err;
965 }
966 #else
967 #define tsc210x_suspend NULL
968 #define tsc210x_resume  NULL
969 #endif
970
971 /* REVISIT don't make these static */
972 static struct platform_device tsc210x_ts_device = {
973         .name           = "tsc210x-ts",
974         .id             = -1,
975 };
976
977 static struct platform_device tsc210x_hwmon_device = {
978         .name           = "tsc210x-hwmon",
979         .id             = -1,
980 };
981
982 static struct platform_device tsc210x_alsa_device = {
983         .name           = "tsc210x-alsa",
984         .id             = -1,
985 };
986
987 static int tsc210x_probe(struct spi_device *spi, enum tsc_type type)
988 {
989         struct tsc210x_config *pdata = spi->dev.platform_data;
990         struct spi_transfer *spi_buffer;
991         struct tsc210x_dev *dev;
992         int reg;
993         int err = 0;
994
995         if (!pdata) {
996                 dev_dbg(&spi->dev, "Platform data not supplied\n");
997                 return -ENOENT;
998         }
999
1000         if (!spi->irq) {
1001                 dev_dbg(&spi->dev, "Invalid irq value\n");
1002                 return -EINVAL;
1003         }
1004
1005         dev = (struct tsc210x_dev *)
1006                 kzalloc(sizeof(struct tsc210x_dev), GFP_KERNEL);
1007         if (!dev) {
1008                 dev_dbg(&spi->dev, "No memory\n");
1009                 return -ENOMEM;
1010         }
1011
1012         dev->pdata = pdata;
1013         dev->pendown = 0;
1014         dev->spi = spi;
1015         dev->kind = type;
1016         dev->queue = create_singlethread_workqueue(spi->dev.driver->name);
1017         if (!dev->queue) {
1018                 dev_dbg(&spi->dev, "Can't make a workqueue\n");
1019                 err = -ENOMEM;
1020                 goto err_queue;
1021         }
1022
1023         mutex_init(&dev->queue_lock);
1024         init_completion(&dev->data_avail);
1025
1026         /* Allocate enough struct spi_transfer's for all requests */
1027         spi_buffer = kzalloc(sizeof(struct spi_transfer) * 16, GFP_KERNEL);
1028         if (!spi_buffer) {
1029                 dev_dbg(&spi->dev, "No memory for SPI buffers\n");
1030                 err = -ENOMEM;
1031                 goto err_buffers;
1032         }
1033
1034         dev->transfers = spi_buffer;
1035         tsc210x_request_alloc(dev, &dev->req_adc, 0,
1036                         TSC210X_TS_X, 4, dev->adc_data,
1037                         tsc210x_coords_report, &spi_buffer);
1038         tsc210x_request_alloc(dev, &dev->req_status, 0,
1039                         TSC210X_TS_STATUS_CTRL, 1, &dev->status,
1040                         tsc210x_status_report, &spi_buffer);
1041         tsc210x_request_alloc(dev, &dev->req_mode, 1,
1042                         TSC210X_TS_ADC_CTRL, 1, NULL,
1043                         tsc210x_complete_dummy, &spi_buffer);
1044         tsc210x_request_alloc(dev, &dev->req_stop, 1,
1045                         TSC210X_TS_ADC_CTRL, 1, NULL,
1046                         tsc210x_complete_dummy, &spi_buffer);
1047
1048         if (pdata->bclk) {
1049                 /* Get the BCLK */
1050                 dev->bclk_ck = clk_get(&spi->dev, pdata->bclk);
1051                 if (IS_ERR(dev->bclk_ck)) {
1052                         err = PTR_ERR(dev->bclk_ck);
1053                         dev_dbg(&spi->dev, "Unable to get '%s': %i\n",
1054                                         pdata->bclk, err);
1055                         goto err_clk;
1056                 }
1057
1058                 clk_enable(dev->bclk_ck);
1059         }
1060
1061         INIT_DELAYED_WORK(&dev->ts_worker, tsc210x_pressure);
1062         INIT_DELAYED_WORK(&dev->sensor_worker, tsc210x_input_scan);
1063
1064         /* Setup the communication bus */
1065         dev_set_drvdata(&spi->dev, dev);
1066         spi->mode = SPI_MODE_1;
1067         spi->bits_per_word = 16;
1068         err = spi_setup(spi);
1069         if (err)
1070                 goto err_spi;
1071
1072         /* Now try to detect the chip, make first contact.  These chips
1073          * don't self-identify, but we can expect that the status register
1074          * reports the ADC is idle and use that as a sanity check.  (It'd
1075          * be even better if we did a soft reset first...)
1076          */
1077         reg = tsc210x_read_sync(dev, TSC210X_TS_ADC_CTRL);
1078         if (reg < 0) {
1079                 err = reg;
1080                 dev_dbg(&dev->spi->dev, "adc_ctrl, err %d\n", err);
1081                 goto err_spi;
1082         }
1083         if (!(reg & (1 << 14))) {
1084                 err = -EIO;
1085                 dev_dbg(&dev->spi->dev, "adc_ctrl, busy? - %04x\n", reg);
1086                 goto err_spi;
1087         }
1088
1089         reg = tsc210x_read_sync(dev, TSC210X_AUDIO3_CTRL);
1090         if (reg < 0) {
1091                 err = reg;
1092                 dev_dbg(&dev->spi->dev, "revision, err %d\n", err);
1093                 goto err_spi;
1094         }
1095         if (reg == 0xffff) {
1096                 err = -ENODEV;
1097                 dev_dbg(&dev->spi->dev, "no device, err %d\n", err);
1098                 goto err_spi;
1099         }
1100         dev_info(&spi->dev, "rev %d, irq %d\n", reg & 0x0007, spi->irq);
1101
1102         err = tsc210x_configure(dev);
1103         if (err)
1104                 goto err_spi;
1105
1106         /* We want no interrupts before configuration succeeds.  */
1107         mutex_lock(&dev->queue_lock);
1108         dev->flushing = 1;
1109
1110         if (request_irq(spi->irq, tsc210x_handler, IRQF_SAMPLE_RANDOM |
1111                                 IRQF_TRIGGER_FALLING, spi->dev.driver->name,
1112                                 dev)) {
1113                 dev_dbg(&spi->dev, "Could not allocate touchscreen IRQ!\n");
1114                 err = -EINVAL;
1115                 goto err_irq;
1116         }
1117
1118         /* Register subdevices controlled by the TSC 2101/2102 */
1119         tsc210x_ts_device.dev.platform_data = dev;
1120         tsc210x_ts_device.dev.parent = &spi->dev;
1121         err = platform_device_register(&tsc210x_ts_device);
1122         if (err)
1123                 goto err_irq;
1124
1125         tsc210x_hwmon_device.dev.platform_data = pdata;
1126         tsc210x_hwmon_device.dev.parent = &spi->dev;
1127         err = platform_device_register(&tsc210x_hwmon_device);
1128         if (err)
1129                 goto err_hwmon;
1130
1131         tsc210x_alsa_device.dev.platform_data = pdata->alsa_config;
1132         tsc210x_alsa_device.dev.parent = &spi->dev;
1133         err = platform_device_register(&tsc210x_alsa_device);
1134         if (err)
1135                 goto err_alsa;
1136
1137         dev->flushing = 0;
1138         mutex_unlock(&dev->queue_lock);
1139         return 0;
1140
1141 err_alsa:
1142         platform_device_unregister(&tsc210x_hwmon_device);
1143 err_hwmon:
1144         platform_device_unregister(&tsc210x_ts_device);
1145 err_irq:
1146         mutex_unlock(&dev->queue_lock);
1147 err_spi:
1148         dev_set_drvdata(&spi->dev, NULL);
1149         clk_disable(dev->bclk_ck);
1150         clk_put(dev->bclk_ck);
1151 err_clk:
1152         kfree(dev->transfers);
1153 err_buffers:
1154         destroy_workqueue(dev->queue);
1155 err_queue:
1156         kfree(dev);
1157         return err;
1158 }
1159
1160 static int tsc2101_probe(struct spi_device *spi)
1161 {
1162         return tsc210x_probe(spi, tsc2101);
1163 }
1164
1165 static int tsc2102_probe(struct spi_device *spi)
1166 {
1167         return tsc210x_probe(spi, tsc2102);
1168 }
1169
1170 static int tsc210x_remove(struct spi_device *spi)
1171 {
1172         struct tsc210x_dev *dev = dev_get_drvdata(&spi->dev);
1173
1174         /* Stop the inputs scan loop */
1175         mutex_lock(&dev->queue_lock);
1176         dev->flushing = 1;
1177         cancel_delayed_work(&dev->sensor_worker);
1178         mutex_unlock(&dev->queue_lock);
1179         flush_workqueue(dev->queue);
1180
1181         /* Wait for pen-up */
1182         while (dev->pendown)
1183                 flush_workqueue(dev->queue);
1184
1185         /* Abort current conversion and power down the ADC */
1186         tsc210x_write_sync(dev, TSC210X_TS_ADC_CTRL, TSC210X_ADC_ADST);
1187         /* NOTE: write_sync() could fail */
1188
1189         destroy_workqueue(dev->queue);
1190
1191         platform_device_unregister(&tsc210x_ts_device);
1192         platform_device_unregister(&tsc210x_hwmon_device);
1193         platform_device_unregister(&tsc210x_alsa_device);
1194
1195         dev_set_drvdata(&spi->dev, NULL);
1196
1197         /* Release the BCLK */
1198         clk_disable(dev->bclk_ck);
1199         clk_put(dev->bclk_ck);
1200
1201         kfree(dev->transfers);
1202         kfree(dev);
1203
1204         return 0;
1205 }
1206
1207 static struct spi_driver tsc2101_driver = {
1208         .probe          = tsc2101_probe,
1209         .remove         = tsc210x_remove,
1210         .suspend        = tsc210x_suspend,
1211         .resume         = tsc210x_resume,
1212         .driver         = {
1213                 .name   = "tsc2101",
1214                 .owner  = THIS_MODULE,
1215                 .bus    = &spi_bus_type,
1216         },
1217 };
1218
1219 static struct spi_driver tsc2102_driver = {
1220         .probe          = tsc2102_probe,
1221         .remove         = tsc210x_remove,
1222         .suspend        = tsc210x_suspend,
1223         .resume         = tsc210x_resume,
1224         .driver         = {
1225                 .name   = "tsc2102",
1226                 .owner  = THIS_MODULE,
1227                 .bus    = &spi_bus_type,
1228         },
1229 };
1230
1231 static char __initdata banner[] = KERN_INFO "TI TSC210x driver initializing\n";
1232
1233 static int __init tsc210x_init(void)
1234 {
1235         int err;
1236         printk(banner);
1237
1238         settings.ts_msecs = 20;
1239         settings.mode_msecs = 1000;
1240
1241         err = spi_register_driver(&tsc2101_driver);
1242         if (err != 0)
1243                 return err;
1244
1245         err = spi_register_driver(&tsc2102_driver);
1246         if (err != 0)
1247                 spi_unregister_driver(&tsc2101_driver);
1248
1249         return err;
1250 }
1251 module_init(tsc210x_init);
1252
1253 static void __exit tsc210x_exit(void)
1254 {
1255         spi_unregister_driver(&tsc2101_driver);
1256         spi_unregister_driver(&tsc2102_driver);
1257 }
1258 module_exit(tsc210x_exit);
1259
1260 MODULE_AUTHOR("Andrzej Zaborowski");
1261 MODULE_DESCRIPTION("Interface driver for TI TSC210x chips.");
1262 MODULE_LICENSE("GPL");