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,
32 #include "Pt1000_temp_data.h"
36 S3C24X0_GPIO * const gpio = S3C24X0_GetBase_GPIO();
37 S3C24X0_SPI * const spi = S3C24X0_GetBase_SPI();
40 /* Configure I/O ports. */
41 gpio->PDCON = (gpio->PDCON & 0xF3FFFF) | 0x040000;
42 gpio->PGCON = (gpio->PGCON & 0x0F3FFF) | 0x008000;
43 gpio->PGCON = (gpio->PGCON & 0x0CFFFF) | 0x020000;
44 gpio->PGCON = (gpio->PGCON & 0x03FFFF) | 0x080000;
48 spi->ch[0].SPPRE = 0x1F; /* Baud-rate ca. 514kHz */
49 spi->ch[0].SPPIN = 0x01; /* SPI-MOSI holds Level after last bit */
50 spi->ch[0].SPCON = 0x1A; /* Polling, Prescaler, Master, CPOL=0,
53 /* Dummy byte ensures clock to be low. */
54 for (i = 0; i < 10; i++) {
55 spi->ch[0].SPTDAT = 0xFF;
57 spi_wait_transmit_done();
61 void spi_wait_transmit_done(void)
63 S3C24X0_SPI * const spi = S3C24X0_GetBase_SPI();
65 while (!(spi->ch[0].SPSTA & 0x01)); /* wait until transfer is done */
69 void tsc2000_write(unsigned short reg, unsigned short data)
71 S3C24X0_SPI * const spi = S3C24X0_GetBase_SPI();
76 spi->ch[0].SPTDAT = (command & 0xFF00) >> 8;
77 spi_wait_transmit_done();
78 spi->ch[0].SPTDAT = (command & 0x00FF);
79 spi_wait_transmit_done();
80 spi->ch[0].SPTDAT = (data & 0xFF00) >> 8;
81 spi_wait_transmit_done();
82 spi->ch[0].SPTDAT = (data & 0x00FF);
83 spi_wait_transmit_done();
89 unsigned short tsc2000_read (unsigned short reg)
91 unsigned short command, data;
92 S3C24X0_SPI * const spi = S3C24X0_GetBase_SPI();
95 command = 0x8000 | reg;
97 spi->ch[0].SPTDAT = (command & 0xFF00) >> 8;
98 spi_wait_transmit_done();
99 spi->ch[0].SPTDAT = (command & 0x00FF);
100 spi_wait_transmit_done();
102 spi->ch[0].SPTDAT = 0xFF;
103 spi_wait_transmit_done();
104 data = spi->ch[0].SPRDAT;
105 spi->ch[0].SPTDAT = 0xFF;
106 spi_wait_transmit_done();
109 return (spi->ch[0].SPRDAT & 0x0FF) | (data << 8);
113 void tsc2000_set_mux (unsigned int channel)
115 S3C24X0_GPIO * const gpio = S3C24X0_GetBase_GPIO();
117 CLR_MUX1_ENABLE; CLR_MUX2_ENABLE;
118 CLR_MUX3_ENABLE; CLR_MUX4_ENABLE;
190 void tsc2000_set_range (unsigned int range)
192 S3C24X0_GPIO * const gpio = S3C24X0_GetBase_GPIO();
196 CLR_SEL_TEMP_V_0; SET_SEL_TEMP_V_1;
197 CLR_SEL_TEMP_V_2; CLR_SEL_TEMP_V_3;
200 CLR_SEL_TEMP_V_0; CLR_SEL_TEMP_V_1;
201 CLR_SEL_TEMP_V_2; SET_SEL_TEMP_V_3;
204 SET_SEL_TEMP_V_0; CLR_SEL_TEMP_V_1;
205 SET_SEL_TEMP_V_2; CLR_SEL_TEMP_V_3;
211 u16 tsc2000_read_channel (unsigned int channel)
215 tsc2000_set_mux(channel);
216 udelay(3 * TSC2000_DELAY_BASE);
218 tsc2000_write(TSC2000_REG_ADC, 0x2036);
219 adc_wait_conversion_done ();
220 res = tsc2000_read(TSC2000_REG_AUX1);
225 s32 tsc2000_contact_temp (void)
227 long adc_pt1000, offset;
233 tsc2000_set_range (3);
235 adc_pt1000 = tsc2000_read_channel (14);
236 debug ("read channel 14 (pt1000 adc value): %ld\n", adc_pt1000);
238 offset = tsc2000_read_channel (15);
239 debug ("read channel 15 (offset): %ld\n", offset);
242 * Formula for calculating voltage drop on PT1000 resistor: u_pt1000 =
243 * x_range3 * (adc_raw - offset) / 10. Formula to calculate x_range3:
244 * x_range3 = (2500 * (1000000 + err_vref + err_amp3)) / (4095*6). The
245 * error correction Values err_vref and err_amp3 are assumed as 0 in
246 * u-boot, because this could cause only a very small error (< 1%).
248 u_pt1000 = (101750 * (adc_pt1000 - offset)) / 10;
249 debug ("u_pt1000: %ld\n", u_pt1000);
251 if (tsc2000_interpolate(u_pt1000, Pt1000_temp_table,
252 &contact_temp) == -1) {
253 printf ("%s: error interpolating PT1000 vlaue\n",
257 debug ("contact_temp: %ld\n", contact_temp);
263 void tsc2000_reg_init (void)
265 S3C24X0_GPIO * const gpio = S3C24X0_GetBase_GPIO();
267 tsc2000_write(TSC2000_REG_ADC, 0x2036);
268 tsc2000_write(TSC2000_REG_REF, 0x0011);
269 tsc2000_write(TSC2000_REG_DACCTL, 0x0000);
285 tsc2000_set_range(0);
289 int tsc2000_interpolate(long value, long data[][2], long *result)
293 /* the data is sorted and the first element is upper
294 * limit so we can easily check for out-of-band values
296 if (data[0][0] < value || data[1][0] > value)
300 while (data[i][0] < value)
303 /* To prevent overflow we have to store the intermediate
304 result in 'long long'.
307 *result = data[i-1][1] +
308 ((unsigned long long)(data[i][1] - data[i-1][1])
309 * (unsigned long long)(value - data[i-1][0]))
310 / (data[i][0] - data[i-1][0]);
316 void adc_wait_conversion_done(void)
318 while (!(tsc2000_read(TSC2000_REG_ADC) & (1 << 14)));