lan9303_i2c.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. * Copyright (C) 2017 Pengutronix, Juergen Borleis <kernel@pengutronix.de>
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * version 2, as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. */
  14. #include <linux/kernel.h>
  15. #include <linux/module.h>
  16. #include <linux/i2c.h>
  17. #include <linux/of.h>
  18. #include "lan9303.h"
  19. struct lan9303_i2c {
  20. struct i2c_client *device;
  21. struct lan9303 chip;
  22. };
  23. static const struct regmap_config lan9303_i2c_regmap_config = {
  24. .reg_bits = 8,
  25. .val_bits = 32,
  26. .reg_stride = 1,
  27. .can_multi_write = true,
  28. .max_register = 0x0ff, /* address bits 0..1 are not used */
  29. .reg_format_endian = REGMAP_ENDIAN_LITTLE,
  30. .volatile_table = &lan9303_register_set,
  31. .wr_table = &lan9303_register_set,
  32. .rd_table = &lan9303_register_set,
  33. .cache_type = REGCACHE_NONE,
  34. };
  35. static int lan9303_i2c_probe(struct i2c_client *client,
  36. const struct i2c_device_id *id)
  37. {
  38. struct lan9303_i2c *sw_dev;
  39. int ret;
  40. sw_dev = devm_kzalloc(&client->dev, sizeof(struct lan9303_i2c),
  41. GFP_KERNEL);
  42. if (!sw_dev)
  43. return -ENOMEM;
  44. sw_dev->chip.regmap = devm_regmap_init_i2c(client,
  45. &lan9303_i2c_regmap_config);
  46. if (IS_ERR(sw_dev->chip.regmap)) {
  47. ret = PTR_ERR(sw_dev->chip.regmap);
  48. dev_err(&client->dev, "Failed to allocate register map: %d\n",
  49. ret);
  50. return ret;
  51. }
  52. /* link forward and backward */
  53. sw_dev->device = client;
  54. i2c_set_clientdata(client, sw_dev);
  55. sw_dev->chip.dev = &client->dev;
  56. sw_dev->chip.ops = &lan9303_indirect_phy_ops;
  57. ret = lan9303_probe(&sw_dev->chip, client->dev.of_node);
  58. if (ret != 0)
  59. return ret;
  60. dev_info(&client->dev, "LAN9303 I2C driver loaded successfully\n");
  61. return 0;
  62. }
  63. static int lan9303_i2c_remove(struct i2c_client *client)
  64. {
  65. struct lan9303_i2c *sw_dev;
  66. sw_dev = i2c_get_clientdata(client);
  67. if (!sw_dev)
  68. return -ENODEV;
  69. return lan9303_remove(&sw_dev->chip);
  70. }
  71. /*-------------------------------------------------------------------------*/
  72. static const struct i2c_device_id lan9303_i2c_id[] = {
  73. { "lan9303", 0 },
  74. { /* sentinel */ }
  75. };
  76. MODULE_DEVICE_TABLE(i2c, lan9303_i2c_id);
  77. static const struct of_device_id lan9303_i2c_of_match[] = {
  78. { .compatible = "smsc,lan9303-i2c", },
  79. { /* sentinel */ },
  80. };
  81. MODULE_DEVICE_TABLE(of, lan9303_i2c_of_match);
  82. static struct i2c_driver lan9303_i2c_driver = {
  83. .driver = {
  84. .name = "LAN9303_I2C",
  85. .of_match_table = of_match_ptr(lan9303_i2c_of_match),
  86. },
  87. .probe = lan9303_i2c_probe,
  88. .remove = lan9303_i2c_remove,
  89. .id_table = lan9303_i2c_id,
  90. };
  91. module_i2c_driver(lan9303_i2c_driver);
  92. MODULE_AUTHOR("Juergen Borleis <kernel@pengutronix.de>");
  93. MODULE_DESCRIPTION("Driver for SMSC/Microchip LAN9303 three port ethernet switch in I2C managed mode");
  94. MODULE_LICENSE("GPL v2");