ds1620.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * linux/drivers/char/ds1620.c: Dallas Semiconductors DS1620
  4. * thermometer driver (as used in the Rebel.com NetWinder)
  5. */
  6. #include <linux/module.h>
  7. #include <linux/miscdevice.h>
  8. #include <linux/delay.h>
  9. #include <linux/proc_fs.h>
  10. #include <linux/seq_file.h>
  11. #include <linux/capability.h>
  12. #include <linux/init.h>
  13. #include <linux/mutex.h>
  14. #include <mach/hardware.h>
  15. #include <asm/mach-types.h>
  16. #include <linux/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 DEFINE_MUTEX(ds1620_mutex);
  35. static const char *fan_state[] = { "off", "on", "on (hardwired)" };
  36. /*
  37. * Start of NetWinder specifics
  38. * Note! We have to hold the gpio lock with IRQs disabled over the
  39. * whole of our transaction to the Dallas chip, since there is a
  40. * chance that the WaveArtist driver could touch these bits to
  41. * enable or disable the speaker.
  42. */
  43. extern unsigned int system_rev;
  44. static inline void netwinder_ds1620_set_clk(int clk)
  45. {
  46. nw_gpio_modify_op(GPIO_DSCLK, clk ? GPIO_DSCLK : 0);
  47. }
  48. static inline void netwinder_ds1620_set_data(int dat)
  49. {
  50. nw_gpio_modify_op(GPIO_DATA, dat ? GPIO_DATA : 0);
  51. }
  52. static inline int netwinder_ds1620_get_data(void)
  53. {
  54. return nw_gpio_read() & GPIO_DATA;
  55. }
  56. static inline void netwinder_ds1620_set_data_dir(int dir)
  57. {
  58. nw_gpio_modify_io(GPIO_DATA, dir ? GPIO_DATA : 0);
  59. }
  60. static inline void netwinder_ds1620_reset(void)
  61. {
  62. nw_cpld_modify(CPLD_DS_ENABLE, 0);
  63. nw_cpld_modify(CPLD_DS_ENABLE, CPLD_DS_ENABLE);
  64. }
  65. static inline void netwinder_lock(unsigned long *flags)
  66. {
  67. raw_spin_lock_irqsave(&nw_gpio_lock, *flags);
  68. }
  69. static inline void netwinder_unlock(unsigned long *flags)
  70. {
  71. raw_spin_unlock_irqrestore(&nw_gpio_lock, *flags);
  72. }
  73. static inline void netwinder_set_fan(int i)
  74. {
  75. unsigned long flags;
  76. raw_spin_lock_irqsave(&nw_gpio_lock, flags);
  77. nw_gpio_modify_op(GPIO_FAN, i ? GPIO_FAN : 0);
  78. raw_spin_unlock_irqrestore(&nw_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 (nw_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 int ds1620_open(struct inode *inode, struct file *file)
  168. {
  169. return stream_open(inode, file);
  170. }
  171. static ssize_t
  172. ds1620_read(struct file *file, char __user *buf, size_t count, loff_t *ptr)
  173. {
  174. signed int cur_temp;
  175. signed char cur_temp_degF;
  176. cur_temp = cvt_9_to_int(ds1620_in(THERM_READ_TEMP, 9)) >> 1;
  177. /* convert to Fahrenheit, as per wdt.c */
  178. cur_temp_degF = (cur_temp * 9) / 5 + 32;
  179. if (copy_to_user(buf, &cur_temp_degF, 1))
  180. return -EFAULT;
  181. return 1;
  182. }
  183. static int
  184. ds1620_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  185. {
  186. struct therm therm;
  187. union {
  188. struct therm __user *therm;
  189. int __user *i;
  190. } uarg;
  191. int i;
  192. uarg.i = (int __user *)arg;
  193. switch(cmd) {
  194. case CMD_SET_THERMOSTATE:
  195. case CMD_SET_THERMOSTATE2:
  196. if (!capable(CAP_SYS_ADMIN))
  197. return -EPERM;
  198. if (cmd == CMD_SET_THERMOSTATE) {
  199. if (get_user(therm.hi, uarg.i))
  200. return -EFAULT;
  201. therm.lo = therm.hi - 3;
  202. } else {
  203. if (copy_from_user(&therm, uarg.therm, sizeof(therm)))
  204. return -EFAULT;
  205. }
  206. therm.lo <<= 1;
  207. therm.hi <<= 1;
  208. ds1620_write_state(&therm);
  209. break;
  210. case CMD_GET_THERMOSTATE:
  211. case CMD_GET_THERMOSTATE2:
  212. ds1620_read_state(&therm);
  213. therm.lo >>= 1;
  214. therm.hi >>= 1;
  215. if (cmd == CMD_GET_THERMOSTATE) {
  216. if (put_user(therm.hi, uarg.i))
  217. return -EFAULT;
  218. } else {
  219. if (copy_to_user(uarg.therm, &therm, sizeof(therm)))
  220. return -EFAULT;
  221. }
  222. break;
  223. case CMD_GET_TEMPERATURE:
  224. case CMD_GET_TEMPERATURE2:
  225. i = cvt_9_to_int(ds1620_in(THERM_READ_TEMP, 9));
  226. if (cmd == CMD_GET_TEMPERATURE)
  227. i >>= 1;
  228. return put_user(i, uarg.i) ? -EFAULT : 0;
  229. case CMD_GET_STATUS:
  230. i = ds1620_in(THERM_READ_CONFIG, 8) & 0xe3;
  231. return put_user(i, uarg.i) ? -EFAULT : 0;
  232. case CMD_GET_FAN:
  233. i = netwinder_get_fan();
  234. return put_user(i, uarg.i) ? -EFAULT : 0;
  235. case CMD_SET_FAN:
  236. if (!capable(CAP_SYS_ADMIN))
  237. return -EPERM;
  238. if (get_user(i, uarg.i))
  239. return -EFAULT;
  240. netwinder_set_fan(i);
  241. break;
  242. default:
  243. return -ENOIOCTLCMD;
  244. }
  245. return 0;
  246. }
  247. static long
  248. ds1620_unlocked_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  249. {
  250. int ret;
  251. mutex_lock(&ds1620_mutex);
  252. ret = ds1620_ioctl(file, cmd, arg);
  253. mutex_unlock(&ds1620_mutex);
  254. return ret;
  255. }
  256. #ifdef THERM_USE_PROC
  257. static int ds1620_proc_therm_show(struct seq_file *m, void *v)
  258. {
  259. struct therm th;
  260. int temp;
  261. ds1620_read_state(&th);
  262. temp = cvt_9_to_int(ds1620_in(THERM_READ_TEMP, 9));
  263. seq_printf(m, "Thermostat: HI %i.%i, LOW %i.%i; temperature: %i.%i C, fan %s\n",
  264. th.hi >> 1, th.hi & 1 ? 5 : 0,
  265. th.lo >> 1, th.lo & 1 ? 5 : 0,
  266. temp >> 1, temp & 1 ? 5 : 0,
  267. fan_state[netwinder_get_fan()]);
  268. return 0;
  269. }
  270. #endif
  271. static const struct file_operations ds1620_fops = {
  272. .owner = THIS_MODULE,
  273. .open = ds1620_open,
  274. .read = ds1620_read,
  275. .unlocked_ioctl = ds1620_unlocked_ioctl,
  276. .llseek = no_llseek,
  277. };
  278. static struct miscdevice ds1620_miscdev = {
  279. TEMP_MINOR,
  280. "temp",
  281. &ds1620_fops
  282. };
  283. static int __init ds1620_init(void)
  284. {
  285. int ret;
  286. struct therm th, th_start;
  287. if (!machine_is_netwinder())
  288. return -ENODEV;
  289. ds1620_out(THERM_RESET, 0, 0);
  290. ds1620_out(THERM_WRITE_CONFIG, 8, CFG_CPU);
  291. ds1620_out(THERM_START_CONVERT, 0, 0);
  292. /*
  293. * Trigger the fan to start by setting
  294. * temperature high point low. This kicks
  295. * the fan into action.
  296. */
  297. ds1620_read_state(&th);
  298. th_start.lo = 0;
  299. th_start.hi = 1;
  300. ds1620_write_state(&th_start);
  301. msleep(2000);
  302. ds1620_write_state(&th);
  303. ret = misc_register(&ds1620_miscdev);
  304. if (ret < 0)
  305. return ret;
  306. #ifdef THERM_USE_PROC
  307. if (!proc_create_single("therm", 0, NULL, ds1620_proc_therm_show))
  308. printk(KERN_ERR "therm: unable to register /proc/therm\n");
  309. #endif
  310. ds1620_read_state(&th);
  311. ret = cvt_9_to_int(ds1620_in(THERM_READ_TEMP, 9));
  312. printk(KERN_INFO "Thermostat: high %i.%i, low %i.%i, "
  313. "current %i.%i C, fan %s.\n",
  314. th.hi >> 1, th.hi & 1 ? 5 : 0,
  315. th.lo >> 1, th.lo & 1 ? 5 : 0,
  316. ret >> 1, ret & 1 ? 5 : 0,
  317. fan_state[netwinder_get_fan()]);
  318. return 0;
  319. }
  320. static void __exit ds1620_exit(void)
  321. {
  322. #ifdef THERM_USE_PROC
  323. remove_proc_entry("therm", NULL);
  324. #endif
  325. misc_deregister(&ds1620_miscdev);
  326. }
  327. module_init(ds1620_init);
  328. module_exit(ds1620_exit);
  329. MODULE_LICENSE("GPL");