7segled.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. * 7 Segment LED routines
  3. * Based on RBTX49xx patch from CELF patch archive.
  4. *
  5. * This file is subject to the terms and conditions of the GNU General Public
  6. * License. See the file "COPYING" in the main directory of this archive
  7. * for more details.
  8. *
  9. * (C) Copyright TOSHIBA CORPORATION 2005-2007
  10. * All Rights Reserved.
  11. */
  12. #include <linux/sysdev.h>
  13. #include <linux/slab.h>
  14. #include <linux/map_to_7segment.h>
  15. #include <asm/txx9/generic.h>
  16. static unsigned int tx_7segled_num;
  17. static void (*tx_7segled_putc)(unsigned int pos, unsigned char val);
  18. void __init txx9_7segled_init(unsigned int num,
  19. void (*putc)(unsigned int pos, unsigned char val))
  20. {
  21. tx_7segled_num = num;
  22. tx_7segled_putc = putc;
  23. }
  24. static SEG7_CONVERSION_MAP(txx9_seg7map, MAP_ASCII7SEG_ALPHANUM_LC);
  25. int txx9_7segled_putc(unsigned int pos, char c)
  26. {
  27. if (pos >= tx_7segled_num)
  28. return -EINVAL;
  29. c = map_to_seg7(&txx9_seg7map, c);
  30. if (c < 0)
  31. return c;
  32. tx_7segled_putc(pos, c);
  33. return 0;
  34. }
  35. static ssize_t ascii_store(struct sys_device *dev,
  36. struct sysdev_attribute *attr,
  37. const char *buf, size_t size)
  38. {
  39. unsigned int ch = dev->id;
  40. txx9_7segled_putc(ch, buf[0]);
  41. return size;
  42. }
  43. static ssize_t raw_store(struct sys_device *dev,
  44. struct sysdev_attribute *attr,
  45. const char *buf, size_t size)
  46. {
  47. unsigned int ch = dev->id;
  48. tx_7segled_putc(ch, buf[0]);
  49. return size;
  50. }
  51. static SYSDEV_ATTR(ascii, 0200, NULL, ascii_store);
  52. static SYSDEV_ATTR(raw, 0200, NULL, raw_store);
  53. static ssize_t map_seg7_show(struct sysdev_class *class, char *buf)
  54. {
  55. memcpy(buf, &txx9_seg7map, sizeof(txx9_seg7map));
  56. return sizeof(txx9_seg7map);
  57. }
  58. static ssize_t map_seg7_store(struct sysdev_class *class,
  59. const char *buf, size_t size)
  60. {
  61. if (size != sizeof(txx9_seg7map))
  62. return -EINVAL;
  63. memcpy(&txx9_seg7map, buf, size);
  64. return size;
  65. }
  66. static SYSDEV_CLASS_ATTR(map_seg7, 0600, map_seg7_show, map_seg7_store);
  67. static struct sysdev_class tx_7segled_sysdev_class = {
  68. .name = "7segled",
  69. };
  70. static int __init tx_7segled_init_sysfs(void)
  71. {
  72. int error, i;
  73. if (!tx_7segled_num)
  74. return -ENODEV;
  75. error = sysdev_class_register(&tx_7segled_sysdev_class);
  76. if (error)
  77. return error;
  78. error = sysdev_class_create_file(&tx_7segled_sysdev_class,
  79. &attr_map_seg7);
  80. if (error)
  81. return error;
  82. for (i = 0; i < tx_7segled_num; i++) {
  83. struct sys_device *dev;
  84. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  85. if (!dev) {
  86. error = -ENODEV;
  87. break;
  88. }
  89. dev->id = i;
  90. dev->cls = &tx_7segled_sysdev_class;
  91. error = sysdev_register(dev);
  92. if (!error) {
  93. sysdev_create_file(dev, &attr_ascii);
  94. sysdev_create_file(dev, &attr_raw);
  95. }
  96. }
  97. return error;
  98. }
  99. device_initcall(tx_7segled_init_sysfs);