ds1620.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. /*
  2. * linux/drivers/char/ds1620.c: Dallas Semiconductors DS1620
  3. * thermometer driver (as used in the Rebel.com NetWinder)
  4. */
  5. #include <linux/module.h>
  6. #include <linux/sched.h>
  7. #include <linux/miscdevice.h>
  8. #include <linux/smp_lock.h>
  9. #include <linux/delay.h>
  10. #include <linux/proc_fs.h>
  11. #include <linux/capability.h>
  12. #include <linux/init.h>
  13. #include <asm/hardware.h>
  14. #include <asm/mach-types.h>
  15. #include <asm/uaccess.h>
  16. #include <asm/therm.h>
  17. #ifdef CONFIG_PROC_FS
  18. /* define for /proc interface */
  19. #define THERM_USE_PROC
  20. #endif
  21. /* Definitions for DS1620 chip */
  22. #define THERM_START_CONVERT 0xee
  23. #define THERM_RESET 0xaf
  24. #define THERM_READ_CONFIG 0xac
  25. #define THERM_READ_TEMP 0xaa
  26. #define THERM_READ_TL 0xa2
  27. #define THERM_READ_TH 0xa1
  28. #define THERM_WRITE_CONFIG 0x0c
  29. #define THERM_WRITE_TL 0x02
  30. #define THERM_WRITE_TH 0x01
  31. #define CFG_CPU 2
  32. #define CFG_1SHOT 1
  33. static const char *fan_state[] = { "off", "on", "on (hardwired)" };
  34. /*
  35. * Start of NetWinder specifics
  36. * Note! We have to hold the gpio lock with IRQs disabled over the
  37. * whole of our transaction to the Dallas chip, since there is a
  38. * chance that the WaveArtist driver could touch these bits to
  39. * enable or disable the speaker.
  40. */
  41. extern spinlock_t gpio_lock;
  42. extern unsigned int system_rev;
  43. static inline void netwinder_ds1620_set_clk(int clk)
  44. {
  45. gpio_modify_op(GPIO_DSCLK, clk ? GPIO_DSCLK : 0);
  46. }
  47. static inline void netwinder_ds1620_set_data(int dat)
  48. {
  49. gpio_modify_op(GPIO_DATA, dat ? GPIO_DATA : 0);
  50. }
  51. static inline int netwinder_ds1620_get_data(void)
  52. {
  53. return gpio_read() & GPIO_DATA;
  54. }
  55. static inline void netwinder_ds1620_set_data_dir(int dir)
  56. {
  57. gpio_modify_io(GPIO_DATA, dir ? GPIO_DATA : 0);
  58. }
  59. static inline void netwinder_ds1620_reset(void)
  60. {
  61. cpld_modify(CPLD_DS_ENABLE, 0);
  62. cpld_modify(CPLD_DS_ENABLE, CPLD_DS_ENABLE);
  63. }
  64. static inline void netwinder_lock(unsigned long *flags)
  65. {
  66. spin_lock_irqsave(&gpio_lock, *flags);
  67. }
  68. static inline void netwinder_unlock(unsigned long *flags)
  69. {
  70. spin_unlock_irqrestore(&gpio_lock, *flags);
  71. }
  72. static inline void netwinder_set_fan(int i)
  73. {
  74. unsigned long flags;
  75. spin_lock_irqsave(&gpio_lock, flags);
  76. gpio_modify_op(GPIO_FAN, i ? GPIO_FAN : 0);
  77. spin_unlock_irqrestore(&gpio_lock, flags);
  78. }
  79. static inline int netwinder_get_fan(void)
  80. {
  81. if ((system_rev & 0xf000) == 0x4000)
  82. return FAN_ALWAYS_ON;
  83. return (gpio_read() & GPIO_FAN) ? FAN_ON : FAN_OFF;
  84. }
  85. /*
  86. * End of NetWinder specifics
  87. */
  88. static void ds1620_send_bits(int nr, int value)
  89. {
  90. int i;
  91. for (i = 0; i < nr; i++) {
  92. netwinder_ds1620_set_data(value & 1);
  93. netwinder_ds1620_set_clk(0);
  94. udelay(1);
  95. netwinder_ds1620_set_clk(1);
  96. udelay(1);
  97. value >>= 1;
  98. }
  99. }
  100. static unsigned int ds1620_recv_bits(int nr)
  101. {
  102. unsigned int value = 0, mask = 1;
  103. int i;
  104. netwinder_ds1620_set_data(0);
  105. for (i = 0; i < nr; i++) {
  106. netwinder_ds1620_set_clk(0);
  107. udelay(1);
  108. if (netwinder_ds1620_get_data())
  109. value |= mask;
  110. mask <<= 1;
  111. netwinder_ds1620_set_clk(1);
  112. udelay(1);
  113. }
  114. return value;
  115. }
  116. static void ds1620_out(int cmd, int bits, int value)
  117. {
  118. unsigned long flags;
  119. netwinder_lock(&flags);
  120. netwinder_ds1620_set_clk(1);
  121. netwinder_ds1620_set_data_dir(0);
  122. netwinder_ds1620_reset();
  123. udelay(1);
  124. ds1620_send_bits(8, cmd);
  125. if (bits)
  126. ds1620_send_bits(bits, value);
  127. udelay(1);
  128. netwinder_ds1620_reset();
  129. netwinder_unlock(&flags);
  130. msleep(20);
  131. }
  132. static unsigned int ds1620_in(int cmd, int bits)
  133. {
  134. unsigned long flags;
  135. unsigned int value;
  136. netwinder_lock(&flags);
  137. netwinder_ds1620_set_clk(1);
  138. netwinder_ds1620_set_data_dir(0);
  139. netwinder_ds1620_reset();
  140. udelay(1);
  141. ds1620_send_bits(8, cmd);
  142. netwinder_ds1620_set_data_dir(1);
  143. value = ds1620_recv_bits(bits);
  144. netwinder_ds1620_reset();
  145. netwinder_unlock(&flags);
  146. return value;
  147. }
  148. static int cvt_9_to_int(unsigned int val)
  149. {
  150. if (val & 0x100)
  151. val |= 0xfffffe00;
  152. return val;
  153. }
  154. static void ds1620_write_state(struct therm *therm)
  155. {
  156. ds1620_out(THERM_WRITE_CONFIG, 8, CFG_CPU);
  157. ds1620_out(THERM_WRITE_TL, 9, therm->lo);
  158. ds1620_out(THERM_WRITE_TH, 9, therm->hi);
  159. ds1620_out(THERM_START_CONVERT, 0, 0);
  160. }
  161. static void ds1620_read_state(struct therm *therm)
  162. {
  163. therm->lo = cvt_9_to_int(ds1620_in(THERM_READ_TL, 9));
  164. therm->hi = cvt_9_to_int(ds1620_in(THERM_READ_TH, 9));
  165. }
  166. static ssize_t
  167. ds1620_read(struct file *file, char __user *buf, size_t count, loff_t *ptr)
  168. {
  169. signed int cur_temp;
  170. signed char cur_temp_degF;
  171. cur_temp = cvt_9_to_int(ds1620_in(THERM_READ_TEMP, 9)) >> 1;
  172. /* convert to Fahrenheit, as per wdt.c */
  173. cur_temp_degF = (cur_temp * 9) / 5 + 32;
  174. if (copy_to_user(buf, &cur_temp_degF, 1))
  175. return -EFAULT;
  176. return 1;
  177. }
  178. static int
  179. ds1620_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg)
  180. {
  181. struct therm therm;
  182. union {
  183. struct therm __user *therm;
  184. int __user *i;
  185. } uarg;
  186. int i;
  187. uarg.i = (int __user *)arg;
  188. switch(cmd) {
  189. case CMD_SET_THERMOSTATE:
  190. case CMD_SET_THERMOSTATE2:
  191. if (!capable(CAP_SYS_ADMIN))
  192. return -EPERM;
  193. if (cmd == CMD_SET_THERMOSTATE) {
  194. if (get_user(therm.hi, uarg.i))
  195. return -EFAULT;
  196. therm.lo = therm.hi - 3;
  197. } else {
  198. if (copy_from_user(&therm, uarg.therm, sizeof(therm)))
  199. return -EFAULT;
  200. }
  201. therm.lo <<= 1;
  202. therm.hi <<= 1;
  203. ds1620_write_state(&therm);
  204. break;
  205. case CMD_GET_THERMOSTATE:
  206. case CMD_GET_THERMOSTATE2:
  207. ds1620_read_state(&therm);
  208. therm.lo >>= 1;
  209. therm.hi >>= 1;
  210. if (cmd == CMD_GET_THERMOSTATE) {
  211. if (put_user(therm.hi, uarg.i))
  212. return -EFAULT;
  213. } else {
  214. if (copy_to_user(uarg.therm, &therm, sizeof(therm)))
  215. return -EFAULT;
  216. }
  217. break;
  218. case CMD_GET_TEMPERATURE:
  219. case CMD_GET_TEMPERATURE2:
  220. i = cvt_9_to_int(ds1620_in(THERM_READ_TEMP, 9));
  221. if (cmd == CMD_GET_TEMPERATURE)
  222. i >>= 1;
  223. return put_user(i, uarg.i) ? -EFAULT : 0;
  224. case CMD_GET_STATUS:
  225. i = ds1620_in(THERM_READ_CONFIG, 8) & 0xe3;
  226. return put_user(i, uarg.i) ? -EFAULT : 0;
  227. case CMD_GET_FAN:
  228. i = netwinder_get_fan();
  229. return put_user(i, uarg.i) ? -EFAULT : 0;
  230. case CMD_SET_FAN:
  231. if (!capable(CAP_SYS_ADMIN))
  232. return -EPERM;
  233. if (get_user(i, uarg.i))
  234. return -EFAULT;
  235. netwinder_set_fan(i);
  236. break;
  237. default:
  238. return -ENOIOCTLCMD;
  239. }
  240. return 0;
  241. }
  242. #ifdef THERM_USE_PROC
  243. static int
  244. proc_therm_ds1620_read(char *buf, char **start, off_t offset,
  245. int len, int *eof, void *unused)
  246. {
  247. struct therm th;
  248. int temp;
  249. ds1620_read_state(&th);
  250. temp = cvt_9_to_int(ds1620_in(THERM_READ_TEMP, 9));
  251. len = sprintf(buf, "Thermostat: HI %i.%i, LOW %i.%i; "
  252. "temperature: %i.%i C, fan %s\n",
  253. th.hi >> 1, th.hi & 1 ? 5 : 0,
  254. th.lo >> 1, th.lo & 1 ? 5 : 0,
  255. temp >> 1, temp & 1 ? 5 : 0,
  256. fan_state[netwinder_get_fan()]);
  257. return len;
  258. }
  259. static struct proc_dir_entry *proc_therm_ds1620;
  260. #endif
  261. static const struct file_operations ds1620_fops = {
  262. .owner = THIS_MODULE,
  263. .open = nonseekable_open,
  264. .read = ds1620_read,
  265. .ioctl = ds1620_ioctl,
  266. };
  267. static struct miscdevice ds1620_miscdev = {
  268. TEMP_MINOR,
  269. "temp",
  270. &ds1620_fops
  271. };
  272. static int __init ds1620_init(void)
  273. {
  274. int ret;
  275. struct therm th, th_start;
  276. if (!machine_is_netwinder())
  277. return -ENODEV;
  278. ds1620_out(THERM_RESET, 0, 0);
  279. ds1620_out(THERM_WRITE_CONFIG, 8, CFG_CPU);
  280. ds1620_out(THERM_START_CONVERT, 0, 0);
  281. /*
  282. * Trigger the fan to start by setting
  283. * temperature high point low. This kicks
  284. * the fan into action.
  285. */
  286. ds1620_read_state(&th);
  287. th_start.lo = 0;
  288. th_start.hi = 1;
  289. ds1620_write_state(&th_start);
  290. msleep(2000);
  291. ds1620_write_state(&th);
  292. ret = misc_register(&ds1620_miscdev);
  293. if (ret < 0)
  294. return ret;
  295. #ifdef THERM_USE_PROC
  296. proc_therm_ds1620 = create_proc_entry("therm", 0, NULL);
  297. if (proc_therm_ds1620)
  298. proc_therm_ds1620->read_proc = proc_therm_ds1620_read;
  299. else
  300. printk(KERN_ERR "therm: unable to register /proc/therm\n");
  301. #endif
  302. ds1620_read_state(&th);
  303. ret = cvt_9_to_int(ds1620_in(THERM_READ_TEMP, 9));
  304. printk(KERN_INFO "Thermostat: high %i.%i, low %i.%i, "
  305. "current %i.%i C, fan %s.\n",
  306. th.hi >> 1, th.hi & 1 ? 5 : 0,
  307. th.lo >> 1, th.lo & 1 ? 5 : 0,
  308. ret >> 1, ret & 1 ? 5 : 0,
  309. fan_state[netwinder_get_fan()]);
  310. return 0;
  311. }
  312. static void __exit ds1620_exit(void)
  313. {
  314. #ifdef THERM_USE_PROC
  315. remove_proc_entry("therm", NULL);
  316. #endif
  317. misc_deregister(&ds1620_miscdev);
  318. }
  319. module_init(ds1620_init);
  320. module_exit(ds1620_exit);
  321. MODULE_LICENSE("GPL");