ds1620.c 8.2 KB

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