7segled.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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/device.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 device *dev,
  36. struct device_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 device *dev,
  44. struct device_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 DEVICE_ATTR(ascii, 0200, NULL, ascii_store);
  52. static DEVICE_ATTR(raw, 0200, NULL, raw_store);
  53. static ssize_t map_seg7_show(struct device *dev,
  54. struct device_attribute *attr,
  55. char *buf)
  56. {
  57. memcpy(buf, &txx9_seg7map, sizeof(txx9_seg7map));
  58. return sizeof(txx9_seg7map);
  59. }
  60. static ssize_t map_seg7_store(struct device *dev,
  61. struct device_attribute *attr,
  62. const char *buf, size_t size)
  63. {
  64. if (size != sizeof(txx9_seg7map))
  65. return -EINVAL;
  66. memcpy(&txx9_seg7map, buf, size);
  67. return size;
  68. }
  69. static DEVICE_ATTR(map_seg7, 0600, map_seg7_show, map_seg7_store);
  70. static struct bus_type tx_7segled_subsys = {
  71. .name = "7segled",
  72. .dev_name = "7segled",
  73. };
  74. static void tx_7segled_release(struct device *dev)
  75. {
  76. kfree(dev);
  77. }
  78. static int __init tx_7segled_init_sysfs(void)
  79. {
  80. int error, i;
  81. if (!tx_7segled_num)
  82. return -ENODEV;
  83. error = subsys_system_register(&tx_7segled_subsys, NULL);
  84. if (error)
  85. return error;
  86. error = device_create_file(tx_7segled_subsys.dev_root, &dev_attr_map_seg7);
  87. if (error)
  88. return error;
  89. for (i = 0; i < tx_7segled_num; i++) {
  90. struct device *dev;
  91. dev = kzalloc(sizeof(*dev), GFP_KERNEL);
  92. if (!dev) {
  93. error = -ENODEV;
  94. break;
  95. }
  96. dev->id = i;
  97. dev->bus = &tx_7segled_subsys;
  98. dev->release = &tx_7segled_release;
  99. error = device_register(dev);
  100. if (error) {
  101. put_device(dev);
  102. return error;
  103. }
  104. device_create_file(dev, &dev_attr_ascii);
  105. device_create_file(dev, &dev_attr_raw);
  106. }
  107. return error;
  108. }
  109. device_initcall(tx_7segled_init_sysfs);