ds1620.c 8.2 KB

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