tsc2000.c 8.5 KB

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