tsc2000.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. /*
  2. * Functions to access the TSC2000 controller on TRAB board (used for scanning
  3. * thermo sensors)
  4. *
  5. * Copyright (C) 2003 Martin Krause, TQ-Systems GmbH, martin.krause@tqs.de
  6. *
  7. * Copyright (C) 2002 DENX Software Engineering, Wolfgang Denk, wd@denx.de
  8. *
  9. * See file CREDITS for list of people who contributed to this
  10. * project.
  11. *
  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.
  16. *
  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.
  21. *
  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,
  25. * MA 02111-1307 USA
  26. */
  27. #include <common.h>
  28. #include <s3c2400.h>
  29. #include "tsc2000.h"
  30. #include "Pt1000_temp_data.h"
  31. /* helper function */
  32. #define abs(value) (((value) < 0) ? ((value)*-1) : (value))
  33. /*
  34. * Maximal allowed deviation between two immediate meassurments of an analog
  35. * thermo channel. 1 DIGIT = 0.0276 °C. This is used to filter sporadic
  36. * "jumps" in measurment.
  37. */
  38. #define MAX_DEVIATION 18 /* unit: DIGITs of adc; 18 DIGIT = 0.5 °C */
  39. void spi_init(void)
  40. {
  41. S3C24X0_GPIO * const gpio = S3C24X0_GetBase_GPIO();
  42. S3C24X0_SPI * const spi = S3C24X0_GetBase_SPI();
  43. int i;
  44. /* Configure I/O ports. */
  45. gpio->PDCON = (gpio->PDCON & 0xF3FFFF) | 0x040000;
  46. gpio->PGCON = (gpio->PGCON & 0x0F3FFF) | 0x008000;
  47. gpio->PGCON = (gpio->PGCON & 0x0CFFFF) | 0x020000;
  48. gpio->PGCON = (gpio->PGCON & 0x03FFFF) | 0x080000;
  49. CLR_CS_TOUCH();
  50. spi->ch[0].SPPRE = 0x1F; /* Baud-rate ca. 514kHz */
  51. spi->ch[0].SPPIN = 0x01; /* SPI-MOSI holds Level after last bit */
  52. spi->ch[0].SPCON = 0x1A; /* Polling, Prescaler, Master, CPOL=0,
  53. CPHA=1 */
  54. /* Dummy byte ensures clock to be low. */
  55. for (i = 0; i < 10; i++) {
  56. spi->ch[0].SPTDAT = 0xFF;
  57. }
  58. spi_wait_transmit_done();
  59. }
  60. void spi_wait_transmit_done(void)
  61. {
  62. S3C24X0_SPI * const spi = S3C24X0_GetBase_SPI();
  63. while (!(spi->ch[0].SPSTA & 0x01)); /* wait until transfer is done */
  64. }
  65. void tsc2000_write(unsigned short reg, unsigned short data)
  66. {
  67. S3C24X0_SPI * const spi = S3C24X0_GetBase_SPI();
  68. unsigned int command;
  69. SET_CS_TOUCH();
  70. command = reg;
  71. spi->ch[0].SPTDAT = (command & 0xFF00) >> 8;
  72. spi_wait_transmit_done();
  73. spi->ch[0].SPTDAT = (command & 0x00FF);
  74. spi_wait_transmit_done();
  75. spi->ch[0].SPTDAT = (data & 0xFF00) >> 8;
  76. spi_wait_transmit_done();
  77. spi->ch[0].SPTDAT = (data & 0x00FF);
  78. spi_wait_transmit_done();
  79. CLR_CS_TOUCH();
  80. }
  81. unsigned short tsc2000_read (unsigned short reg)
  82. {
  83. unsigned short command, data;
  84. S3C24X0_SPI * const spi = S3C24X0_GetBase_SPI();
  85. SET_CS_TOUCH();
  86. command = 0x8000 | reg;
  87. spi->ch[0].SPTDAT = (command & 0xFF00) >> 8;
  88. spi_wait_transmit_done();
  89. spi->ch[0].SPTDAT = (command & 0x00FF);
  90. spi_wait_transmit_done();
  91. spi->ch[0].SPTDAT = 0xFF;
  92. spi_wait_transmit_done();
  93. data = spi->ch[0].SPRDAT;
  94. spi->ch[0].SPTDAT = 0xFF;
  95. spi_wait_transmit_done();
  96. CLR_CS_TOUCH();
  97. return (spi->ch[0].SPRDAT & 0x0FF) | (data << 8);
  98. }
  99. void tsc2000_set_mux (unsigned int channel)
  100. {
  101. S3C24X0_GPIO * const gpio = S3C24X0_GetBase_GPIO();
  102. CLR_MUX1_ENABLE; CLR_MUX2_ENABLE;
  103. CLR_MUX3_ENABLE; CLR_MUX4_ENABLE;
  104. switch (channel) {
  105. case 0:
  106. CLR_MUX0; CLR_MUX1;
  107. SET_MUX1_ENABLE;
  108. break;
  109. case 1:
  110. SET_MUX0; CLR_MUX1;
  111. SET_MUX1_ENABLE;
  112. break;
  113. case 2:
  114. CLR_MUX0; SET_MUX1;
  115. SET_MUX1_ENABLE;
  116. break;
  117. case 3:
  118. SET_MUX0; SET_MUX1;
  119. SET_MUX1_ENABLE;
  120. break;
  121. case 4:
  122. CLR_MUX0; CLR_MUX1;
  123. SET_MUX2_ENABLE;
  124. break;
  125. case 5:
  126. SET_MUX0; CLR_MUX1;
  127. SET_MUX2_ENABLE;
  128. break;
  129. case 6:
  130. CLR_MUX0; SET_MUX1;
  131. SET_MUX2_ENABLE;
  132. break;
  133. case 7:
  134. SET_MUX0; SET_MUX1;
  135. SET_MUX2_ENABLE;
  136. break;
  137. case 8:
  138. CLR_MUX0; CLR_MUX1;
  139. SET_MUX3_ENABLE;
  140. break;
  141. case 9:
  142. SET_MUX0; CLR_MUX1;
  143. SET_MUX3_ENABLE;
  144. break;
  145. case 10:
  146. CLR_MUX0; SET_MUX1;
  147. SET_MUX3_ENABLE;
  148. break;
  149. case 11:
  150. SET_MUX0; SET_MUX1;
  151. SET_MUX3_ENABLE;
  152. break;
  153. case 12:
  154. CLR_MUX0; CLR_MUX1;
  155. SET_MUX4_ENABLE;
  156. break;
  157. case 13:
  158. SET_MUX0; CLR_MUX1;
  159. SET_MUX4_ENABLE;
  160. break;
  161. case 14:
  162. CLR_MUX0; SET_MUX1;
  163. SET_MUX4_ENABLE;
  164. break;
  165. case 15:
  166. SET_MUX0; SET_MUX1;
  167. SET_MUX4_ENABLE;
  168. break;
  169. default:
  170. CLR_MUX0; CLR_MUX1;
  171. }
  172. }
  173. void tsc2000_set_range (unsigned int range)
  174. {
  175. S3C24X0_GPIO * const gpio = S3C24X0_GetBase_GPIO();
  176. switch (range) {
  177. case 1:
  178. CLR_SEL_TEMP_V_0; SET_SEL_TEMP_V_1;
  179. CLR_SEL_TEMP_V_2; CLR_SEL_TEMP_V_3;
  180. break;
  181. case 2:
  182. CLR_SEL_TEMP_V_0; CLR_SEL_TEMP_V_1;
  183. CLR_SEL_TEMP_V_2; SET_SEL_TEMP_V_3;
  184. break;
  185. case 3:
  186. SET_SEL_TEMP_V_0; CLR_SEL_TEMP_V_1;
  187. SET_SEL_TEMP_V_2; CLR_SEL_TEMP_V_3;
  188. break;
  189. }
  190. }
  191. u16 tsc2000_read_channel (unsigned int channel)
  192. {
  193. u16 res;
  194. tsc2000_set_mux(channel);
  195. udelay(3 * TSC2000_DELAY_BASE);
  196. tsc2000_write(TSC2000_REG_ADC, 0x2036);
  197. adc_wait_conversion_done ();
  198. res = tsc2000_read(TSC2000_REG_AUX1);
  199. return res;
  200. }
  201. s32 tsc2000_contact_temp (void)
  202. {
  203. long adc_pt1000, offset;
  204. long u_pt1000;
  205. long contact_temp;
  206. long temp1, temp2;
  207. tsc2000_reg_init ();
  208. tsc2000_set_range (3);
  209. /*
  210. * Because of sporadic "jumps" in the measured adc values every
  211. * channel is read two times. If there is a significant difference
  212. * between the two measurements, then print an error and do a third
  213. * measurement, because it is very unlikely that a successive third
  214. * measurement goes also wrong.
  215. */
  216. temp1 = tsc2000_read_channel (14);
  217. temp2 = tsc2000_read_channel (14);
  218. if (abs(temp2 - temp1) < MAX_DEVIATION)
  219. adc_pt1000 = temp2;
  220. else {
  221. printf ("%s: read adc value (channel 14) exceeded max allowed "
  222. "deviation: %d * 0.0276 °C\n",
  223. __FUNCTION__, MAX_DEVIATION);
  224. printf ("adc value 1: %ld DIGITs\nadc value 2: %ld DIGITs\n",
  225. temp1, temp2);
  226. adc_pt1000 = tsc2000_read_channel (14);
  227. printf ("use (third read) adc value: adc_pt1000 = "
  228. "%ld DIGITs\n", adc_pt1000);
  229. }
  230. debug ("read channel 14 (pt1000 adc value): %ld\n", adc_pt1000);
  231. temp1 = tsc2000_read_channel (15);
  232. temp2 = tsc2000_read_channel (15);
  233. if (abs(temp2 - temp1) < MAX_DEVIATION)
  234. offset = temp2;
  235. else {
  236. printf ("%s: read adc value (channel 15) exceeded max allowed "
  237. "deviation: %d * 0.0276 °C\n",
  238. __FUNCTION__, MAX_DEVIATION);
  239. printf ("adc value 1: %ld DIGITs\nadc value 2: %ld DIGITs\n",
  240. temp1, temp2);
  241. offset = tsc2000_read_channel (15);
  242. printf ("use (third read) adc value: offset = %ld DIGITs\n",
  243. offset);
  244. }
  245. debug ("read channel 15 (offset): %ld\n", offset);
  246. /*
  247. * Formula for calculating voltage drop on PT1000 resistor: u_pt1000 =
  248. * x_range3 * (adc_raw - offset) / 10. Formula to calculate x_range3:
  249. * x_range3 = (2500 * (1000000 + err_vref + err_amp3)) / (4095*6). The
  250. * error correction Values err_vref and err_amp3 are assumed as 0 in
  251. * u-boot, because this could cause only a very small error (< 1%).
  252. */
  253. u_pt1000 = (101750 * (adc_pt1000 - offset)) / 10;
  254. debug ("u_pt1000: %ld\n", u_pt1000);
  255. if (tsc2000_interpolate(u_pt1000, Pt1000_temp_table,
  256. &contact_temp) == -1) {
  257. printf ("%s: error interpolating PT1000 vlaue\n",
  258. __FUNCTION__);
  259. return (-1000);
  260. }
  261. debug ("contact_temp: %ld\n", contact_temp);
  262. return contact_temp;
  263. }
  264. void tsc2000_reg_init (void)
  265. {
  266. 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);
  270. CON_MUX0;
  271. CON_MUX1;
  272. CON_MUX1_ENABLE;
  273. CON_MUX2_ENABLE;
  274. CON_MUX3_ENABLE;
  275. CON_MUX4_ENABLE;
  276. CON_SEL_TEMP_V_0;
  277. CON_SEL_TEMP_V_1;
  278. CON_SEL_TEMP_V_2;
  279. CON_SEL_TEMP_V_3;
  280. tsc2000_set_mux(0);
  281. tsc2000_set_range(0);
  282. }
  283. int tsc2000_interpolate(long value, long data[][2], long *result)
  284. {
  285. int i;
  286. /* the data is sorted and the first element is upper
  287. * limit so we can easily check for out-of-band values
  288. */
  289. if (data[0][0] < value || data[1][0] > value)
  290. return -1;
  291. i = 1;
  292. while (data[i][0] < value)
  293. i++;
  294. /* To prevent overflow we have to store the intermediate
  295. result in 'long long'.
  296. */
  297. *result = data[i-1][1] +
  298. ((unsigned long long)(data[i][1] - data[i-1][1])
  299. * (unsigned long long)(value - data[i-1][0]))
  300. / (data[i][0] - data[i-1][0]);
  301. return 0;
  302. }
  303. void adc_wait_conversion_done(void)
  304. {
  305. while (!(tsc2000_read(TSC2000_REG_ADC) & (1 << 14)));
  306. }