1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (C) 2021-2022, Intel Corporation. */
8 * ice_gnss_do_write - Write data to internal GNSS receiver
9 * @pf: board private structure
10 * @buf: command buffer
11 * @size: command buffer size
13 * Write UBX command data to the GNSS receiver
16 * * number of bytes written - success
17 * * negative - error code
20 ice_gnss_do_write(struct ice_pf *pf, const unsigned char *buf, unsigned int size)
22 struct ice_aqc_link_topo_addr link_topo;
23 struct ice_hw *hw = &pf->hw;
24 unsigned int offset = 0;
27 memset(&link_topo, 0, sizeof(struct ice_aqc_link_topo_addr));
28 link_topo.topo_params.index = ICE_E810T_GNSS_I2C_BUS;
29 link_topo.topo_params.node_type_ctx |=
30 FIELD_PREP(ICE_AQC_LINK_TOPO_NODE_CTX_M,
31 ICE_AQC_LINK_TOPO_NODE_CTX_OVERRIDE);
33 /* It's not possible to write a single byte to u-blox.
34 * Write all bytes in a loop until there are 6 or less bytes left. If
35 * there are exactly 6 bytes left, the last write would be only a byte.
36 * In this case, do 4+2 bytes writes instead of 5+1. Otherwise, do the
37 * last 2 to 5 bytes write.
39 while (size - offset > ICE_GNSS_UBX_WRITE_BYTES + 1) {
40 err = ice_aq_write_i2c(hw, link_topo, ICE_GNSS_UBX_I2C_BUS_ADDR,
41 cpu_to_le16(buf[offset]),
42 ICE_MAX_I2C_WRITE_BYTES,
43 &buf[offset + 1], NULL);
47 offset += ICE_GNSS_UBX_WRITE_BYTES;
50 /* Single byte would be written. Write 4 bytes instead of 5. */
51 if (size - offset == ICE_GNSS_UBX_WRITE_BYTES + 1) {
52 err = ice_aq_write_i2c(hw, link_topo, ICE_GNSS_UBX_I2C_BUS_ADDR,
53 cpu_to_le16(buf[offset]),
54 ICE_MAX_I2C_WRITE_BYTES - 1,
55 &buf[offset + 1], NULL);
59 offset += ICE_GNSS_UBX_WRITE_BYTES - 1;
62 /* Do the last write, 2 to 5 bytes. */
63 err = ice_aq_write_i2c(hw, link_topo, ICE_GNSS_UBX_I2C_BUS_ADDR,
64 cpu_to_le16(buf[offset]), size - offset - 1,
65 &buf[offset + 1], NULL);
72 dev_err(ice_pf_to_dev(pf), "GNSS failed to write, offset=%u, size=%u, err=%d\n",
79 * ice_gnss_read - Read data from internal GNSS module
80 * @work: GNSS read work structure
82 * Read the data from internal GNSS receiver, write it to gnss_dev.
84 static void ice_gnss_read(struct kthread_work *work)
86 struct gnss_serial *gnss = container_of(work, struct gnss_serial,
88 unsigned long delay = ICE_GNSS_POLL_DATA_DELAY_TIME;
89 unsigned int i, bytes_read, data_len, count;
90 struct ice_aqc_link_topo_addr link_topo;
99 if (!pf || !test_bit(ICE_FLAG_GNSS, pf->flags))
104 memset(&link_topo, 0, sizeof(struct ice_aqc_link_topo_addr));
105 link_topo.topo_params.index = ICE_E810T_GNSS_I2C_BUS;
106 link_topo.topo_params.node_type_ctx |=
107 FIELD_PREP(ICE_AQC_LINK_TOPO_NODE_CTX_M,
108 ICE_AQC_LINK_TOPO_NODE_CTX_OVERRIDE);
110 i2c_params = ICE_GNSS_UBX_DATA_LEN_WIDTH |
111 ICE_AQC_I2C_USE_REPEATED_START;
113 err = ice_aq_read_i2c(hw, link_topo, ICE_GNSS_UBX_I2C_BUS_ADDR,
114 cpu_to_le16(ICE_GNSS_UBX_DATA_LEN_H),
115 i2c_params, (u8 *)&data_len_b, NULL);
119 data_len = be16_to_cpu(data_len_b);
120 if (data_len == 0 || data_len == U16_MAX)
123 /* The u-blox has data_len bytes for us to read */
125 data_len = min_t(typeof(data_len), data_len, PAGE_SIZE);
127 buf = (char *)get_zeroed_page(GFP_KERNEL);
133 /* Read received data */
134 for (i = 0; i < data_len; i += bytes_read) {
135 unsigned int bytes_left = data_len - i;
137 bytes_read = min_t(typeof(bytes_left), bytes_left,
138 ICE_MAX_I2C_DATA_SIZE);
140 err = ice_aq_read_i2c(hw, link_topo, ICE_GNSS_UBX_I2C_BUS_ADDR,
141 cpu_to_le16(ICE_GNSS_UBX_EMPTY_DATA),
142 bytes_read, &buf[i], NULL);
147 count = gnss_insert_raw(pf->gnss_dev, buf, i);
149 dev_warn(ice_pf_to_dev(pf),
150 "gnss_insert_raw ret=%d size=%d\n",
152 delay = ICE_GNSS_TIMER_DELAY_TIME;
154 free_page((unsigned long)buf);
156 kthread_queue_delayed_work(gnss->kworker, &gnss->read_work, delay);
158 dev_dbg(ice_pf_to_dev(pf), "GNSS failed to read err=%d\n", err);
162 * ice_gnss_struct_init - Initialize GNSS receiver
163 * @pf: Board private structure
165 * Initialize GNSS structures and workers.
168 * * pointer to initialized gnss_serial struct - success
171 static struct gnss_serial *ice_gnss_struct_init(struct ice_pf *pf)
173 struct device *dev = ice_pf_to_dev(pf);
174 struct kthread_worker *kworker;
175 struct gnss_serial *gnss;
177 gnss = kzalloc(sizeof(*gnss), GFP_KERNEL);
182 pf->gnss_serial = gnss;
184 kthread_init_delayed_work(&gnss->read_work, ice_gnss_read);
185 kworker = kthread_create_worker(0, "ice-gnss-%s", dev_name(dev));
186 if (IS_ERR(kworker)) {
191 gnss->kworker = kworker;
197 * ice_gnss_open - Open GNSS device
198 * @gdev: pointer to the gnss device struct
200 * Open GNSS device and start filling the read buffer for consumer.
204 * * negative - error code
206 static int ice_gnss_open(struct gnss_device *gdev)
208 struct ice_pf *pf = gnss_get_drvdata(gdev);
209 struct gnss_serial *gnss;
214 if (!test_bit(ICE_FLAG_GNSS, pf->flags))
217 gnss = pf->gnss_serial;
221 kthread_queue_delayed_work(gnss->kworker, &gnss->read_work, 0);
227 * ice_gnss_close - Close GNSS device
228 * @gdev: pointer to the gnss device struct
230 * Close GNSS device, cancel worker, stop filling the read buffer.
232 static void ice_gnss_close(struct gnss_device *gdev)
234 struct ice_pf *pf = gnss_get_drvdata(gdev);
235 struct gnss_serial *gnss;
240 gnss = pf->gnss_serial;
244 kthread_cancel_delayed_work_sync(&gnss->read_work);
248 * ice_gnss_write - Write to GNSS device
249 * @gdev: pointer to the gnss device struct
250 * @buf: pointer to the user data
251 * @count: size of the buffer to be sent to the GNSS device
254 * * number of written bytes - success
255 * * negative - error code
258 ice_gnss_write(struct gnss_device *gdev, const unsigned char *buf,
261 struct ice_pf *pf = gnss_get_drvdata(gdev);
262 struct gnss_serial *gnss;
264 /* We cannot write a single byte using our I2C implementation. */
265 if (count <= 1 || count > ICE_GNSS_TTY_WRITE_BUF)
271 if (!test_bit(ICE_FLAG_GNSS, pf->flags))
274 gnss = pf->gnss_serial;
278 return ice_gnss_do_write(pf, buf, count);
281 static const struct gnss_operations ice_gnss_ops = {
282 .open = ice_gnss_open,
283 .close = ice_gnss_close,
284 .write_raw = ice_gnss_write,
288 * ice_gnss_register - Register GNSS receiver
289 * @pf: Board private structure
291 * Allocate and register GNSS receiver in the Linux GNSS subsystem.
295 * * negative - error code
297 static int ice_gnss_register(struct ice_pf *pf)
299 struct gnss_device *gdev;
302 gdev = gnss_allocate_device(ice_pf_to_dev(pf));
304 dev_err(ice_pf_to_dev(pf),
305 "gnss_allocate_device returns NULL\n");
309 gdev->ops = &ice_gnss_ops;
310 gdev->type = GNSS_TYPE_UBX;
311 gnss_set_drvdata(gdev, pf);
312 ret = gnss_register_device(gdev);
314 dev_err(ice_pf_to_dev(pf), "gnss_register_device err=%d\n",
316 gnss_put_device(gdev);
325 * ice_gnss_deregister - Deregister GNSS receiver
326 * @pf: Board private structure
328 * Deregister GNSS receiver from the Linux GNSS subsystem,
329 * release its resources.
331 static void ice_gnss_deregister(struct ice_pf *pf)
334 gnss_deregister_device(pf->gnss_dev);
335 gnss_put_device(pf->gnss_dev);
341 * ice_gnss_init - Initialize GNSS support
342 * @pf: Board private structure
344 void ice_gnss_init(struct ice_pf *pf)
348 pf->gnss_serial = ice_gnss_struct_init(pf);
349 if (!pf->gnss_serial)
352 ret = ice_gnss_register(pf);
354 set_bit(ICE_FLAG_GNSS, pf->flags);
355 dev_info(ice_pf_to_dev(pf), "GNSS init successful\n");
358 dev_err(ice_pf_to_dev(pf), "GNSS init failure\n");
363 * ice_gnss_exit - Disable GNSS TTY support
364 * @pf: Board private structure
366 void ice_gnss_exit(struct ice_pf *pf)
368 ice_gnss_deregister(pf);
369 clear_bit(ICE_FLAG_GNSS, pf->flags);
371 if (pf->gnss_serial) {
372 struct gnss_serial *gnss = pf->gnss_serial;
374 kthread_cancel_delayed_work_sync(&gnss->read_work);
375 kthread_destroy_worker(gnss->kworker);
376 gnss->kworker = NULL;
379 pf->gnss_serial = NULL;
384 * ice_gnss_is_gps_present - Check if GPS HW is present
385 * @hw: pointer to HW struct
387 bool ice_gnss_is_gps_present(struct ice_hw *hw)
389 if (!hw->func_caps.ts_func_info.src_tmr_owned)
392 if (!ice_is_gps_in_netlist(hw))
395 #if IS_ENABLED(CONFIG_PTP_1588_CLOCK)
396 if (ice_is_e810t(hw)) {
400 err = ice_read_pca9575_reg_e810t(hw, ICE_PCA9575_P0_IN, &data);
401 if (err || !!(data & ICE_E810T_P0_GNSS_PRSNT_N))
407 if (!ice_is_e810t(hw))
409 #endif /* IS_ENABLED(CONFIG_PTP_1588_CLOCK) */