tsc2000.c 8.4 KB

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