lan9303_i2c.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2017 Pengutronix, Juergen Borleis <kernel@pengutronix.de>
  4. */
  5. #include <linux/kernel.h>
  6. #include <linux/module.h>
  7. #include <linux/i2c.h>
  8. #include <linux/of.h>
  9. #include "lan9303.h"
  10. struct lan9303_i2c {
  11. struct i2c_client *device;
  12. struct lan9303 chip;
  13. };
  14. static const struct regmap_config lan9303_i2c_regmap_config = {
  15. .reg_bits = 8,
  16. .val_bits = 32,
  17. .reg_stride = 1,
  18. .can_multi_write = true,
  19. .max_register = 0x0ff, /* address bits 0..1 are not used */
  20. .reg_format_endian = REGMAP_ENDIAN_LITTLE,
  21. .volatile_table = &lan9303_register_set,
  22. .wr_table = &lan9303_register_set,
  23. .rd_table = &lan9303_register_set,
  24. .cache_type = REGCACHE_NONE,
  25. };
  26. static int lan9303_i2c_probe(struct i2c_client *client,
  27. const struct i2c_device_id *id)
  28. {
  29. struct lan9303_i2c *sw_dev;
  30. int ret;
  31. sw_dev = devm_kzalloc(&client->dev, sizeof(struct lan9303_i2c),
  32. GFP_KERNEL);
  33. if (!sw_dev)
  34. return -ENOMEM;
  35. sw_dev->chip.regmap = devm_regmap_init_i2c(client,
  36. &lan9303_i2c_regmap_config);
  37. if (IS_ERR(sw_dev->chip.regmap)) {
  38. ret = PTR_ERR(sw_dev->chip.regmap);
  39. dev_err(&client->dev, "Failed to allocate register map: %d\n",
  40. ret);
  41. return ret;
  42. }
  43. /* link forward and backward */
  44. sw_dev->device = client;
  45. i2c_set_clientdata(client, sw_dev);
  46. sw_dev->chip.dev = &client->dev;
  47. sw_dev->chip.ops = &lan9303_indirect_phy_ops;
  48. ret = lan9303_probe(&sw_dev->chip, client->dev.of_node);
  49. if (ret != 0)
  50. return ret;
  51. dev_info(&client->dev, "LAN9303 I2C driver loaded successfully\n");
  52. return 0;
  53. }
  54. static int lan9303_i2c_remove(struct i2c_client *client)
  55. {
  56. struct lan9303_i2c *sw_dev;
  57. sw_dev = i2c_get_clientdata(client);
  58. if (!sw_dev)
  59. return -ENODEV;
  60. return lan9303_remove(&sw_dev->chip);
  61. }
  62. /*-------------------------------------------------------------------------*/
  63. static const struct i2c_device_id lan9303_i2c_id[] = {
  64. { "lan9303", 0 },
  65. { /* sentinel */ }
  66. };
  67. MODULE_DEVICE_TABLE(i2c, lan9303_i2c_id);
  68. static const struct of_device_id lan9303_i2c_of_match[] = {
  69. { .compatible = "smsc,lan9303-i2c", },
  70. { /* sentinel */ },
  71. };
  72. MODULE_DEVICE_TABLE(of, lan9303_i2c_of_match);
  73. static struct i2c_driver lan9303_i2c_driver = {
  74. .driver = {
  75. .name = "LAN9303_I2C",
  76. .of_match_table = of_match_ptr(lan9303_i2c_of_match),
  77. },
  78. .probe = lan9303_i2c_probe,
  79. .remove = lan9303_i2c_remove,
  80. .id_table = lan9303_i2c_id,
  81. };
  82. module_i2c_driver(lan9303_i2c_driver);
  83. MODULE_AUTHOR("Juergen Borleis <kernel@pengutronix.de>");
  84. MODULE_DESCRIPTION("Driver for SMSC/Microchip LAN9303 three port ethernet switch in I2C managed mode");
  85. MODULE_LICENSE("GPL v2");