1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright (C) 2020 NovaTech LLC 4 * George McCollister <george.mccollister@gmail.com> 5 */ 6 7 #include <linux/bits.h> 8 #include <linux/i2c.h> 9 #include <linux/module.h> 10 #include "xrs700x.h" 11 #include "xrs700x_reg.h" 12 13 static int xrs700x_i2c_reg_read(void *context, unsigned int reg, 14 unsigned int *val) 15 { 16 struct device *dev = context; 17 struct i2c_client *i2c = to_i2c_client(dev); 18 unsigned char buf[4]; 19 int ret; 20 21 buf[0] = reg >> 23 & 0xff; 22 buf[1] = reg >> 15 & 0xff; 23 buf[2] = reg >> 7 & 0xff; 24 buf[3] = (reg & 0x7f) << 1; 25 26 ret = i2c_master_send(i2c, buf, sizeof(buf)); 27 if (ret < 0) { 28 dev_err(dev, "xrs i2c_master_send returned %d\n", ret); 29 return ret; 30 } 31 32 ret = i2c_master_recv(i2c, buf, 2); 33 if (ret < 0) { 34 dev_err(dev, "xrs i2c_master_recv returned %d\n", ret); 35 return ret; 36 } 37 38 *val = buf[0] << 8 | buf[1]; 39 40 return 0; 41 } 42 43 static int xrs700x_i2c_reg_write(void *context, unsigned int reg, 44 unsigned int val) 45 { 46 struct device *dev = context; 47 struct i2c_client *i2c = to_i2c_client(dev); 48 unsigned char buf[6]; 49 int ret; 50 51 buf[0] = reg >> 23 & 0xff; 52 buf[1] = reg >> 15 & 0xff; 53 buf[2] = reg >> 7 & 0xff; 54 buf[3] = (reg & 0x7f) << 1 | 1; 55 buf[4] = val >> 8 & 0xff; 56 buf[5] = val & 0xff; 57 58 ret = i2c_master_send(i2c, buf, sizeof(buf)); 59 if (ret < 0) { 60 dev_err(dev, "xrs i2c_master_send returned %d\n", ret); 61 return ret; 62 } 63 64 return 0; 65 } 66 67 static const struct regmap_config xrs700x_i2c_regmap_config = { 68 .val_bits = 16, 69 .reg_stride = 2, 70 .reg_bits = 32, 71 .pad_bits = 0, 72 .write_flag_mask = 0, 73 .read_flag_mask = 0, 74 .reg_read = xrs700x_i2c_reg_read, 75 .reg_write = xrs700x_i2c_reg_write, 76 .max_register = 0, 77 .cache_type = REGCACHE_NONE, 78 .reg_format_endian = REGMAP_ENDIAN_BIG, 79 .val_format_endian = REGMAP_ENDIAN_BIG 80 }; 81 82 static int xrs700x_i2c_probe(struct i2c_client *i2c, 83 const struct i2c_device_id *i2c_id) 84 { 85 struct xrs700x *priv; 86 int ret; 87 88 priv = xrs700x_switch_alloc(&i2c->dev, i2c); 89 if (!priv) 90 return -ENOMEM; 91 92 priv->regmap = devm_regmap_init(&i2c->dev, NULL, &i2c->dev, 93 &xrs700x_i2c_regmap_config); 94 if (IS_ERR(priv->regmap)) { 95 ret = PTR_ERR(priv->regmap); 96 dev_err(&i2c->dev, "Failed to initialize regmap: %d\n", ret); 97 return ret; 98 } 99 100 i2c_set_clientdata(i2c, priv); 101 102 ret = xrs700x_switch_register(priv); 103 104 /* Main DSA driver may not be started yet. */ 105 if (ret) 106 return ret; 107 108 return 0; 109 } 110 111 static int xrs700x_i2c_remove(struct i2c_client *i2c) 112 { 113 struct xrs700x *priv = i2c_get_clientdata(i2c); 114 115 xrs700x_switch_remove(priv); 116 117 return 0; 118 } 119 120 static const struct i2c_device_id xrs700x_i2c_id[] = { 121 { "xrs700x-switch", 0 }, 122 {}, 123 }; 124 125 MODULE_DEVICE_TABLE(i2c, xrs700x_i2c_id); 126 127 static const struct of_device_id xrs700x_i2c_dt_ids[] = { 128 { .compatible = "arrow,xrs7003e", .data = &xrs7003e_info }, 129 { .compatible = "arrow,xrs7003f", .data = &xrs7003f_info }, 130 { .compatible = "arrow,xrs7004e", .data = &xrs7004e_info }, 131 { .compatible = "arrow,xrs7004f", .data = &xrs7004f_info }, 132 {}, 133 }; 134 MODULE_DEVICE_TABLE(of, xrs700x_i2c_dt_ids); 135 136 static struct i2c_driver xrs700x_i2c_driver = { 137 .driver = { 138 .name = "xrs700x-i2c", 139 .of_match_table = of_match_ptr(xrs700x_i2c_dt_ids), 140 }, 141 .probe = xrs700x_i2c_probe, 142 .remove = xrs700x_i2c_remove, 143 .id_table = xrs700x_i2c_id, 144 }; 145 146 module_i2c_driver(xrs700x_i2c_driver); 147 148 MODULE_AUTHOR("George McCollister <george.mccollister@gmail.com>"); 149 MODULE_DESCRIPTION("Arrow SpeedChips XRS700x DSA I2C driver"); 150 MODULE_LICENSE("GPL v2"); 151