sparcspkr.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. /*
  2. * Driver for PC-speaker like devices found on various Sparc systems.
  3. *
  4. * Copyright (c) 2002 Vojtech Pavlik
  5. * Copyright (c) 2002, 2006, 2008 David S. Miller (davem@davemloft.net)
  6. */
  7. #include <linux/kernel.h>
  8. #include <linux/module.h>
  9. #include <linux/init.h>
  10. #include <linux/input.h>
  11. #include <linux/of_device.h>
  12. #include <linux/slab.h>
  13. #include <asm/io.h>
  14. MODULE_AUTHOR("David S. Miller <davem@davemloft.net>");
  15. MODULE_DESCRIPTION("Sparc Speaker beeper driver");
  16. MODULE_LICENSE("GPL");
  17. struct grover_beep_info {
  18. void __iomem *freq_regs;
  19. void __iomem *enable_reg;
  20. };
  21. struct bbc_beep_info {
  22. u32 clock_freq;
  23. void __iomem *regs;
  24. };
  25. struct sparcspkr_state {
  26. const char *name;
  27. int (*event)(struct input_dev *dev, unsigned int type, unsigned int code, int value);
  28. spinlock_t lock;
  29. struct input_dev *input_dev;
  30. union {
  31. struct grover_beep_info grover;
  32. struct bbc_beep_info bbc;
  33. } u;
  34. };
  35. static u32 bbc_count_to_reg(struct bbc_beep_info *info, unsigned int count)
  36. {
  37. u32 val, clock_freq = info->clock_freq;
  38. int i;
  39. if (!count)
  40. return 0;
  41. if (count <= clock_freq >> 20)
  42. return 1 << 18;
  43. if (count >= clock_freq >> 12)
  44. return 1 << 10;
  45. val = 1 << 18;
  46. for (i = 19; i >= 11; i--) {
  47. val >>= 1;
  48. if (count <= clock_freq >> i)
  49. break;
  50. }
  51. return val;
  52. }
  53. static int bbc_spkr_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
  54. {
  55. struct sparcspkr_state *state = dev_get_drvdata(dev->dev.parent);
  56. struct bbc_beep_info *info = &state->u.bbc;
  57. unsigned int count = 0;
  58. unsigned long flags;
  59. if (type != EV_SND)
  60. return -1;
  61. switch (code) {
  62. case SND_BELL: if (value) value = 1000;
  63. case SND_TONE: break;
  64. default: return -1;
  65. }
  66. if (value > 20 && value < 32767)
  67. count = 1193182 / value;
  68. count = bbc_count_to_reg(info, count);
  69. spin_lock_irqsave(&state->lock, flags);
  70. if (count) {
  71. sbus_writeb(0x01, info->regs + 0);
  72. sbus_writeb(0x00, info->regs + 2);
  73. sbus_writeb((count >> 16) & 0xff, info->regs + 3);
  74. sbus_writeb((count >> 8) & 0xff, info->regs + 4);
  75. sbus_writeb(0x00, info->regs + 5);
  76. } else {
  77. sbus_writeb(0x00, info->regs + 0);
  78. }
  79. spin_unlock_irqrestore(&state->lock, flags);
  80. return 0;
  81. }
  82. static int grover_spkr_event(struct input_dev *dev, unsigned int type, unsigned int code, int value)
  83. {
  84. struct sparcspkr_state *state = dev_get_drvdata(dev->dev.parent);
  85. struct grover_beep_info *info = &state->u.grover;
  86. unsigned int count = 0;
  87. unsigned long flags;
  88. if (type != EV_SND)
  89. return -1;
  90. switch (code) {
  91. case SND_BELL: if (value) value = 1000;
  92. case SND_TONE: break;
  93. default: return -1;
  94. }
  95. if (value > 20 && value < 32767)
  96. count = 1193182 / value;
  97. spin_lock_irqsave(&state->lock, flags);
  98. if (count) {
  99. /* enable counter 2 */
  100. sbus_writeb(sbus_readb(info->enable_reg) | 3, info->enable_reg);
  101. /* set command for counter 2, 2 byte write */
  102. sbus_writeb(0xB6, info->freq_regs + 1);
  103. /* select desired HZ */
  104. sbus_writeb(count & 0xff, info->freq_regs + 0);
  105. sbus_writeb((count >> 8) & 0xff, info->freq_regs + 0);
  106. } else {
  107. /* disable counter 2 */
  108. sbus_writeb(sbus_readb(info->enable_reg) & 0xFC, info->enable_reg);
  109. }
  110. spin_unlock_irqrestore(&state->lock, flags);
  111. return 0;
  112. }
  113. static int sparcspkr_probe(struct device *dev)
  114. {
  115. struct sparcspkr_state *state = dev_get_drvdata(dev);
  116. struct input_dev *input_dev;
  117. int error;
  118. input_dev = input_allocate_device();
  119. if (!input_dev)
  120. return -ENOMEM;
  121. input_dev->name = state->name;
  122. input_dev->phys = "sparc/input0";
  123. input_dev->id.bustype = BUS_ISA;
  124. input_dev->id.vendor = 0x001f;
  125. input_dev->id.product = 0x0001;
  126. input_dev->id.version = 0x0100;
  127. input_dev->dev.parent = dev;
  128. input_dev->evbit[0] = BIT_MASK(EV_SND);
  129. input_dev->sndbit[0] = BIT_MASK(SND_BELL) | BIT_MASK(SND_TONE);
  130. input_dev->event = state->event;
  131. error = input_register_device(input_dev);
  132. if (error) {
  133. input_free_device(input_dev);
  134. return error;
  135. }
  136. state->input_dev = input_dev;
  137. return 0;
  138. }
  139. static void sparcspkr_shutdown(struct platform_device *dev)
  140. {
  141. struct sparcspkr_state *state = platform_get_drvdata(dev);
  142. struct input_dev *input_dev = state->input_dev;
  143. /* turn off the speaker */
  144. state->event(input_dev, EV_SND, SND_BELL, 0);
  145. }
  146. static int bbc_beep_probe(struct platform_device *op)
  147. {
  148. struct sparcspkr_state *state;
  149. struct bbc_beep_info *info;
  150. struct device_node *dp;
  151. int err = -ENOMEM;
  152. state = kzalloc(sizeof(*state), GFP_KERNEL);
  153. if (!state)
  154. goto out_err;
  155. state->name = "Sparc BBC Speaker";
  156. state->event = bbc_spkr_event;
  157. spin_lock_init(&state->lock);
  158. dp = of_find_node_by_path("/");
  159. err = -ENODEV;
  160. if (!dp)
  161. goto out_free;
  162. info = &state->u.bbc;
  163. info->clock_freq = of_getintprop_default(dp, "clock-frequency", 0);
  164. if (!info->clock_freq)
  165. goto out_free;
  166. info->regs = of_ioremap(&op->resource[0], 0, 6, "bbc beep");
  167. if (!info->regs)
  168. goto out_free;
  169. platform_set_drvdata(op, state);
  170. err = sparcspkr_probe(&op->dev);
  171. if (err)
  172. goto out_clear_drvdata;
  173. return 0;
  174. out_clear_drvdata:
  175. of_iounmap(&op->resource[0], info->regs, 6);
  176. out_free:
  177. kfree(state);
  178. out_err:
  179. return err;
  180. }
  181. static int bbc_remove(struct platform_device *op)
  182. {
  183. struct sparcspkr_state *state = platform_get_drvdata(op);
  184. struct input_dev *input_dev = state->input_dev;
  185. struct bbc_beep_info *info = &state->u.bbc;
  186. /* turn off the speaker */
  187. state->event(input_dev, EV_SND, SND_BELL, 0);
  188. input_unregister_device(input_dev);
  189. of_iounmap(&op->resource[0], info->regs, 6);
  190. kfree(state);
  191. return 0;
  192. }
  193. static const struct of_device_id bbc_beep_match[] = {
  194. {
  195. .name = "beep",
  196. .compatible = "SUNW,bbc-beep",
  197. },
  198. {},
  199. };
  200. MODULE_DEVICE_TABLE(of, bbc_beep_match);
  201. static struct platform_driver bbc_beep_driver = {
  202. .driver = {
  203. .name = "bbcbeep",
  204. .of_match_table = bbc_beep_match,
  205. },
  206. .probe = bbc_beep_probe,
  207. .remove = bbc_remove,
  208. .shutdown = sparcspkr_shutdown,
  209. };
  210. static int grover_beep_probe(struct platform_device *op)
  211. {
  212. struct sparcspkr_state *state;
  213. struct grover_beep_info *info;
  214. int err = -ENOMEM;
  215. state = kzalloc(sizeof(*state), GFP_KERNEL);
  216. if (!state)
  217. goto out_err;
  218. state->name = "Sparc Grover Speaker";
  219. state->event = grover_spkr_event;
  220. spin_lock_init(&state->lock);
  221. info = &state->u.grover;
  222. info->freq_regs = of_ioremap(&op->resource[2], 0, 2, "grover beep freq");
  223. if (!info->freq_regs)
  224. goto out_free;
  225. info->enable_reg = of_ioremap(&op->resource[3], 0, 1, "grover beep enable");
  226. if (!info->enable_reg)
  227. goto out_unmap_freq_regs;
  228. platform_set_drvdata(op, state);
  229. err = sparcspkr_probe(&op->dev);
  230. if (err)
  231. goto out_clear_drvdata;
  232. return 0;
  233. out_clear_drvdata:
  234. of_iounmap(&op->resource[3], info->enable_reg, 1);
  235. out_unmap_freq_regs:
  236. of_iounmap(&op->resource[2], info->freq_regs, 2);
  237. out_free:
  238. kfree(state);
  239. out_err:
  240. return err;
  241. }
  242. static int grover_remove(struct platform_device *op)
  243. {
  244. struct sparcspkr_state *state = platform_get_drvdata(op);
  245. struct grover_beep_info *info = &state->u.grover;
  246. struct input_dev *input_dev = state->input_dev;
  247. /* turn off the speaker */
  248. state->event(input_dev, EV_SND, SND_BELL, 0);
  249. input_unregister_device(input_dev);
  250. of_iounmap(&op->resource[3], info->enable_reg, 1);
  251. of_iounmap(&op->resource[2], info->freq_regs, 2);
  252. kfree(state);
  253. return 0;
  254. }
  255. static const struct of_device_id grover_beep_match[] = {
  256. {
  257. .name = "beep",
  258. .compatible = "SUNW,smbus-beep",
  259. },
  260. {},
  261. };
  262. MODULE_DEVICE_TABLE(of, grover_beep_match);
  263. static struct platform_driver grover_beep_driver = {
  264. .driver = {
  265. .name = "groverbeep",
  266. .of_match_table = grover_beep_match,
  267. },
  268. .probe = grover_beep_probe,
  269. .remove = grover_remove,
  270. .shutdown = sparcspkr_shutdown,
  271. };
  272. static int __init sparcspkr_init(void)
  273. {
  274. int err = platform_driver_register(&bbc_beep_driver);
  275. if (!err) {
  276. err = platform_driver_register(&grover_beep_driver);
  277. if (err)
  278. platform_driver_unregister(&bbc_beep_driver);
  279. }
  280. return err;
  281. }
  282. static void __exit sparcspkr_exit(void)
  283. {
  284. platform_driver_unregister(&bbc_beep_driver);
  285. platform_driver_unregister(&grover_beep_driver);
  286. }
  287. module_init(sparcspkr_init);
  288. module_exit(sparcspkr_exit);