max6959.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * MAX6958/6959 7-segment LED display controller
  4. * Datasheet:
  5. * https://www.analog.com/media/en/technical-documentation/data-sheets/MAX6958-MAX6959.pdf
  6. *
  7. * Copyright (c) 2024, Intel Corporation.
  8. * Author: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
  9. */
  10. #include <linux/array_size.h>
  11. #include <linux/bitrev.h>
  12. #include <linux/bits.h>
  13. #include <linux/container_of.h>
  14. #include <linux/device/devres.h>
  15. #include <linux/err.h>
  16. #include <linux/i2c.h>
  17. #include <linux/mod_devicetable.h>
  18. #include <linux/module.h>
  19. #include <linux/pm.h>
  20. #include <linux/regmap.h>
  21. #include <linux/types.h>
  22. #include <linux/workqueue.h>
  23. #include <linux/map_to_7segment.h>
  24. #include "line-display.h"
  25. /* Registers */
  26. #define REG_DECODE_MODE 0x01
  27. #define REG_INTENSITY 0x02
  28. #define REG_SCAN_LIMIT 0x03
  29. #define REG_CONFIGURATION 0x04
  30. #define REG_CONFIGURATION_S_BIT BIT(0)
  31. #define REG_DIGIT(x) (0x20 + (x))
  32. #define REG_DIGIT0 0x20
  33. #define REG_DIGIT1 0x21
  34. #define REG_DIGIT2 0x22
  35. #define REG_DIGIT3 0x23
  36. #define REG_SEGMENTS 0x24
  37. #define REG_MAX REG_SEGMENTS
  38. struct max6959_priv {
  39. struct linedisp linedisp;
  40. struct delayed_work work;
  41. struct regmap *regmap;
  42. };
  43. static void max6959_disp_update(struct work_struct *work)
  44. {
  45. struct max6959_priv *priv = container_of(work, struct max6959_priv, work.work);
  46. struct linedisp *linedisp = &priv->linedisp;
  47. struct linedisp_map *map = linedisp->map;
  48. char *s = linedisp->buf;
  49. u8 buf[4];
  50. /* Map segments according to datasheet */
  51. buf[0] = bitrev8(map_to_seg7(&map->map.seg7, *s++)) >> 1;
  52. buf[1] = bitrev8(map_to_seg7(&map->map.seg7, *s++)) >> 1;
  53. buf[2] = bitrev8(map_to_seg7(&map->map.seg7, *s++)) >> 1;
  54. buf[3] = bitrev8(map_to_seg7(&map->map.seg7, *s++)) >> 1;
  55. regmap_bulk_write(priv->regmap, REG_DIGIT(0), buf, ARRAY_SIZE(buf));
  56. }
  57. static int max6959_linedisp_get_map_type(struct linedisp *linedisp)
  58. {
  59. struct max6959_priv *priv = container_of(linedisp, struct max6959_priv, linedisp);
  60. INIT_DELAYED_WORK(&priv->work, max6959_disp_update);
  61. return LINEDISP_MAP_SEG7;
  62. }
  63. static void max6959_linedisp_update(struct linedisp *linedisp)
  64. {
  65. struct max6959_priv *priv = container_of(linedisp, struct max6959_priv, linedisp);
  66. schedule_delayed_work(&priv->work, 0);
  67. }
  68. static const struct linedisp_ops max6959_linedisp_ops = {
  69. .get_map_type = max6959_linedisp_get_map_type,
  70. .update = max6959_linedisp_update,
  71. };
  72. static int max6959_enable(struct max6959_priv *priv, bool enable)
  73. {
  74. u8 mask = REG_CONFIGURATION_S_BIT;
  75. u8 value = enable ? mask : 0;
  76. return regmap_update_bits(priv->regmap, REG_CONFIGURATION, mask, value);
  77. }
  78. static void max6959_power_off(void *priv)
  79. {
  80. max6959_enable(priv, false);
  81. }
  82. static int max6959_power_on(struct max6959_priv *priv)
  83. {
  84. struct device *dev = regmap_get_device(priv->regmap);
  85. int ret;
  86. ret = max6959_enable(priv, true);
  87. if (ret)
  88. return ret;
  89. return devm_add_action_or_reset(dev, max6959_power_off, priv);
  90. }
  91. static const struct regmap_config max6959_regmap_config = {
  92. .reg_bits = 8,
  93. .val_bits = 8,
  94. .max_register = REG_MAX,
  95. .cache_type = REGCACHE_MAPLE,
  96. };
  97. static int max6959_i2c_probe(struct i2c_client *client)
  98. {
  99. struct device *dev = &client->dev;
  100. struct max6959_priv *priv;
  101. int ret;
  102. priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
  103. if (!priv)
  104. return -ENOMEM;
  105. priv->regmap = devm_regmap_init_i2c(client, &max6959_regmap_config);
  106. if (IS_ERR(priv->regmap))
  107. return PTR_ERR(priv->regmap);
  108. ret = max6959_power_on(priv);
  109. if (ret)
  110. return ret;
  111. ret = linedisp_register(&priv->linedisp, dev, 4, &max6959_linedisp_ops);
  112. if (ret)
  113. return ret;
  114. i2c_set_clientdata(client, priv);
  115. return 0;
  116. }
  117. static void max6959_i2c_remove(struct i2c_client *client)
  118. {
  119. struct max6959_priv *priv = i2c_get_clientdata(client);
  120. cancel_delayed_work_sync(&priv->work);
  121. linedisp_unregister(&priv->linedisp);
  122. }
  123. static int max6959_suspend(struct device *dev)
  124. {
  125. return max6959_enable(dev_get_drvdata(dev), false);
  126. }
  127. static int max6959_resume(struct device *dev)
  128. {
  129. return max6959_enable(dev_get_drvdata(dev), true);
  130. }
  131. static DEFINE_SIMPLE_DEV_PM_OPS(max6959_pm_ops, max6959_suspend, max6959_resume);
  132. static const struct i2c_device_id max6959_i2c_id[] = {
  133. { "max6959" },
  134. { }
  135. };
  136. MODULE_DEVICE_TABLE(i2c, max6959_i2c_id);
  137. static const struct of_device_id max6959_of_table[] = {
  138. { .compatible = "maxim,max6959" },
  139. { }
  140. };
  141. MODULE_DEVICE_TABLE(of, max6959_of_table);
  142. static struct i2c_driver max6959_i2c_driver = {
  143. .driver = {
  144. .name = "max6959",
  145. .pm = pm_sleep_ptr(&max6959_pm_ops),
  146. .of_match_table = max6959_of_table,
  147. },
  148. .probe = max6959_i2c_probe,
  149. .remove = max6959_i2c_remove,
  150. .id_table = max6959_i2c_id,
  151. };
  152. module_i2c_driver(max6959_i2c_driver);
  153. MODULE_DESCRIPTION("MAX6958/6959 7-segment LED controller");
  154. MODULE_AUTHOR("Andy Shevchenko <andriy.shevchenko@linux.intel.com>");
  155. MODULE_LICENSE("GPL");
  156. MODULE_IMPORT_NS("LINEDISP");