max6875.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * max6875.c - driver for MAX6874/MAX6875
  4. *
  5. * Copyright (C) 2005 Ben Gardner <bgardner@wabtec.com>
  6. *
  7. * Based on eeprom.c
  8. *
  9. * The MAX6875 has a bank of registers and two banks of EEPROM.
  10. * Address ranges are defined as follows:
  11. * * 0x0000 - 0x0046 = configuration registers
  12. * * 0x8000 - 0x8046 = configuration EEPROM
  13. * * 0x8100 - 0x82FF = user EEPROM
  14. *
  15. * This driver makes the user EEPROM available for read.
  16. *
  17. * The registers & config EEPROM should be accessed via i2c-dev.
  18. *
  19. * The MAX6875 ignores the lowest address bit, so each chip responds to
  20. * two addresses - 0x50/0x51 and 0x52/0x53.
  21. *
  22. * Note that the MAX6875 uses i2c_smbus_write_byte_data() to set the read
  23. * address, so this driver is destructive if loaded for the wrong EEPROM chip.
  24. */
  25. #include <linux/kernel.h>
  26. #include <linux/module.h>
  27. #include <linux/slab.h>
  28. #include <linux/i2c.h>
  29. #include <linux/mutex.h>
  30. /* The MAX6875 can only read/write 16 bytes at a time */
  31. #define SLICE_SIZE 16
  32. #define SLICE_BITS 4
  33. /* USER EEPROM is at addresses 0x8100 - 0x82FF */
  34. #define USER_EEPROM_BASE 0x8100
  35. #define USER_EEPROM_SIZE 0x0200
  36. #define USER_EEPROM_SLICES 32
  37. /* MAX6875 commands */
  38. #define MAX6875_CMD_BLK_READ 0x84
  39. /* Each client has this additional data */
  40. struct max6875_data {
  41. struct i2c_client *fake_client;
  42. struct mutex update_lock;
  43. u32 valid;
  44. u8 data[USER_EEPROM_SIZE];
  45. unsigned long last_updated[USER_EEPROM_SLICES];
  46. };
  47. static void max6875_update_slice(struct i2c_client *client, int slice)
  48. {
  49. struct max6875_data *data = i2c_get_clientdata(client);
  50. int i, j, addr;
  51. u8 *buf;
  52. if (slice >= USER_EEPROM_SLICES)
  53. return;
  54. mutex_lock(&data->update_lock);
  55. buf = &data->data[slice << SLICE_BITS];
  56. if (!(data->valid & (1 << slice)) ||
  57. time_after(jiffies, data->last_updated[slice])) {
  58. dev_dbg(&client->dev, "Starting update of slice %u\n", slice);
  59. data->valid &= ~(1 << slice);
  60. addr = USER_EEPROM_BASE + (slice << SLICE_BITS);
  61. /* select the eeprom address */
  62. if (i2c_smbus_write_byte_data(client, addr >> 8, addr & 0xFF)) {
  63. dev_err(&client->dev, "address set failed\n");
  64. goto exit_up;
  65. }
  66. if (i2c_check_functionality(client->adapter,
  67. I2C_FUNC_SMBUS_READ_I2C_BLOCK)) {
  68. if (i2c_smbus_read_i2c_block_data(client,
  69. MAX6875_CMD_BLK_READ,
  70. SLICE_SIZE,
  71. buf) != SLICE_SIZE) {
  72. goto exit_up;
  73. }
  74. } else {
  75. for (i = 0; i < SLICE_SIZE; i++) {
  76. j = i2c_smbus_read_byte(client);
  77. if (j < 0) {
  78. goto exit_up;
  79. }
  80. buf[i] = j;
  81. }
  82. }
  83. data->last_updated[slice] = jiffies;
  84. data->valid |= (1 << slice);
  85. }
  86. exit_up:
  87. mutex_unlock(&data->update_lock);
  88. }
  89. static ssize_t max6875_read(struct file *filp, struct kobject *kobj,
  90. struct bin_attribute *bin_attr,
  91. char *buf, loff_t off, size_t count)
  92. {
  93. struct i2c_client *client = kobj_to_i2c_client(kobj);
  94. struct max6875_data *data = i2c_get_clientdata(client);
  95. int slice, max_slice;
  96. /* refresh slices which contain requested bytes */
  97. max_slice = (off + count - 1) >> SLICE_BITS;
  98. for (slice = (off >> SLICE_BITS); slice <= max_slice; slice++)
  99. max6875_update_slice(client, slice);
  100. memcpy(buf, &data->data[off], count);
  101. return count;
  102. }
  103. static const struct bin_attribute user_eeprom_attr = {
  104. .attr = {
  105. .name = "eeprom",
  106. .mode = S_IRUGO,
  107. },
  108. .size = USER_EEPROM_SIZE,
  109. .read = max6875_read,
  110. };
  111. static int max6875_probe(struct i2c_client *client,
  112. const struct i2c_device_id *id)
  113. {
  114. struct i2c_adapter *adapter = client->adapter;
  115. struct max6875_data *data;
  116. int err;
  117. if (!i2c_check_functionality(adapter, I2C_FUNC_SMBUS_WRITE_BYTE_DATA
  118. | I2C_FUNC_SMBUS_READ_BYTE))
  119. return -ENODEV;
  120. /* Only bind to even addresses */
  121. if (client->addr & 1)
  122. return -ENODEV;
  123. data = kzalloc(sizeof(struct max6875_data), GFP_KERNEL);
  124. if (!data)
  125. return -ENOMEM;
  126. /* A fake client is created on the odd address */
  127. data->fake_client = i2c_new_dummy_device(client->adapter, client->addr + 1);
  128. if (IS_ERR(data->fake_client)) {
  129. err = PTR_ERR(data->fake_client);
  130. goto exit_kfree;
  131. }
  132. /* Init real i2c_client */
  133. i2c_set_clientdata(client, data);
  134. mutex_init(&data->update_lock);
  135. err = sysfs_create_bin_file(&client->dev.kobj, &user_eeprom_attr);
  136. if (err)
  137. goto exit_remove_fake;
  138. return 0;
  139. exit_remove_fake:
  140. i2c_unregister_device(data->fake_client);
  141. exit_kfree:
  142. kfree(data);
  143. return err;
  144. }
  145. static int max6875_remove(struct i2c_client *client)
  146. {
  147. struct max6875_data *data = i2c_get_clientdata(client);
  148. i2c_unregister_device(data->fake_client);
  149. sysfs_remove_bin_file(&client->dev.kobj, &user_eeprom_attr);
  150. kfree(data);
  151. return 0;
  152. }
  153. static const struct i2c_device_id max6875_id[] = {
  154. { "max6875", 0 },
  155. { }
  156. };
  157. MODULE_DEVICE_TABLE(i2c, max6875_id);
  158. static struct i2c_driver max6875_driver = {
  159. .driver = {
  160. .name = "max6875",
  161. },
  162. .probe = max6875_probe,
  163. .remove = max6875_remove,
  164. .id_table = max6875_id,
  165. };
  166. module_i2c_driver(max6875_driver);
  167. MODULE_AUTHOR("Ben Gardner <bgardner@wabtec.com>");
  168. MODULE_DESCRIPTION("MAX6875 driver");
  169. MODULE_LICENSE("GPL");