2 * Functions to access the TSC2000 controller on TRAB board (used for scanning
5 * Copyright (C) 2003 Martin Krause, TQ-Systems GmbH, martin.krause@tqs.de
7 * Copyright (C) 2002 DENX Software Engineering, Wolfgang Denk, wd@denx.de
9 * See file CREDITS for list of people who contributed to this
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License as
14 * published by the Free Software Foundation; either version 2 of
15 * the License, or (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
34 S3C24X0_GPIO * const gpio = S3C24X0_GetBase_GPIO();
35 S3C24X0_SPI * const spi = S3C24X0_GetBase_SPI();
38 /* Configure I/O ports. */
39 gpio->PDCON = (gpio->PDCON & 0xF3FFFF) | 0x040000;
40 gpio->PGCON = (gpio->PGCON & 0x0F3FFF) | 0x008000;
41 gpio->PGCON = (gpio->PGCON & 0x0CFFFF) | 0x020000;
42 gpio->PGCON = (gpio->PGCON & 0x03FFFF) | 0x080000;
46 spi->ch[0].SPPRE = 0x1F; /* Baud-rate ca. 514kHz */
47 spi->ch[0].SPPIN = 0x01; /* SPI-MOSI holds Level after last bit */
48 spi->ch[0].SPCON = 0x1A; /* Polling, Prescaler, Master, CPOL=0,
51 /* Dummy byte ensures clock to be low. */
52 for (i = 0; i < 10; i++) {
53 spi->ch[0].SPTDAT = 0xFF;
55 spi_wait_transmit_done();
59 static void spi_wait_transmit_done(void)
61 S3C24X0_SPI * const spi = S3C24X0_GetBase_SPI();
63 while (!(spi->ch[0].SPSTA & 0x01)); /* wait until transfer is done */
67 static void tsc2000_write(unsigned short reg, unsigned short data)
69 S3C24X0_SPI * const spi = S3C24X0_GetBase_SPI();
74 spi->ch[0].SPTDAT = (command & 0xFF00) >> 8;
75 spi_wait_transmit_done();
76 spi->ch[0].SPTDAT = (command & 0x00FF);
77 spi_wait_transmit_done();
78 spi->ch[0].SPTDAT = (data & 0xFF00) >> 8;
79 spi_wait_transmit_done();
80 spi->ch[0].SPTDAT = (data & 0x00FF);
81 spi_wait_transmit_done();
87 static unsigned short tsc2000_read (unsigned short reg)
89 unsigned short command, data;
90 S3C24X0_SPI * const spi = S3C24X0_GetBase_SPI();
93 command = 0x8000 | reg;
95 spi->ch[0].SPTDAT = (command & 0xFF00) >> 8;
96 spi_wait_transmit_done();
97 spi->ch[0].SPTDAT = (command & 0x00FF);
98 spi_wait_transmit_done();
100 spi->ch[0].SPTDAT = 0xFF;
101 spi_wait_transmit_done();
102 data = spi->ch[0].SPRDAT;
103 spi->ch[0].SPTDAT = 0xFF;
104 spi_wait_transmit_done();
107 return (spi->ch[0].SPRDAT & 0x0FF) | (data << 8);
111 static void tsc2000_set_mux (unsigned int channel)
113 S3C24X0_GPIO * const gpio = S3C24X0_GetBase_GPIO();
115 CLR_MUX1_ENABLE; CLR_MUX2_ENABLE;
116 CLR_MUX3_ENABLE; CLR_MUX4_ENABLE;
188 static void tsc2000_set_range (unsigned int range)
190 S3C24X0_GPIO * const gpio = S3C24X0_GetBase_GPIO();
194 CLR_SEL_TEMP_V_0; SET_SEL_TEMP_V_1;
195 CLR_SEL_TEMP_V_2; CLR_SEL_TEMP_V_3;
198 CLR_SEL_TEMP_V_0; CLR_SEL_TEMP_V_1;
199 CLR_SEL_TEMP_V_2; SET_SEL_TEMP_V_3;
202 SET_SEL_TEMP_V_0; CLR_SEL_TEMP_V_1;
203 SET_SEL_TEMP_V_2; CLR_SEL_TEMP_V_3;
209 static u16 tsc2000_read_channel (unsigned int channel)
213 tsc2000_set_mux(channel);
214 udelay(3 * TSC2000_DELAY_BASE);
216 tsc2000_write(TSC2000_REG_ADC, 0x2036);
217 adc_wait_conversion_done ();
218 res = tsc2000_read(TSC2000_REG_AUX1);
223 s32 tsc2000_contact_temp (void)
225 long adc_pt1000, offset;
231 tsc2000_set_range (3);
233 adc_pt1000 = tsc2000_read_channel (14);
234 debug ("read channel 14 (pt1000 adc value): %ld\n", adc_pt1000);
236 offset = tsc2000_read_channel (15);
237 debug ("read channel 15 (offset): %ld\n", offset);
240 * Formula for calculating voltage drop on PT1000 resistor: u_pt1000 =
241 * x_range3 * (adc_raw - offset) / 10. Formula to calculate x_range3:
242 * x_range3 = (2500 * (1000000 + err_vref + err_amp3)) / (4095*6). The
243 * error correction Values err_vref and err_amp3 are assumed as 0 in
244 * u-boot, because this could cause only a very small error (< 1%).
246 u_pt1000 = (101750 * (adc_pt1000 - offset)) / 10;
247 debug ("u_pt1000: %ld\n", u_pt1000);
249 if (tsc2000_interpolate(u_pt1000, Pt1000_temp_table,
250 &contact_temp) == -1) {
251 printf ("%s: error interpolating PT1000 vlaue\n",
255 debug ("contact_temp: %ld\n", contact_temp);
261 void tsc2000_reg_init (void)
263 S3C24X0_GPIO * const gpio = S3C24X0_GetBase_GPIO();
265 tsc2000_write(TSC2000_REG_ADC, 0x2036);
266 tsc2000_write(TSC2000_REG_REF, 0x0011);
267 tsc2000_write(TSC2000_REG_DACCTL, 0x0000);
283 tsc2000_set_range(0);
287 static int tsc2000_interpolate(long value, long data[][2], long *result)
291 /* the data is sorted and the first element is upper
292 * limit so we can easily check for out-of-band values
294 if (data[0][0] < value || data[1][0] > value)
298 while (data[i][0] < value)
301 /* To prevent overflow we have to store the intermediate
302 result in 'long long'.
305 *result = data[i-1][1] +
306 ((unsigned long long)(data[i][1] - data[i-1][1])
307 * (unsigned long long)(value - data[i-1][0]))
308 / (data[i][0] - data[i-1][0]);
314 static void adc_wait_conversion_done(void)
316 while (!(tsc2000_read(TSC2000_REG_ADC) & (1 << 14)));