Merge tag 'riscv-for-linus-4.15-rc2_cleanups' of git://git.kernel.org/pub/scm/linux...
[linux/fpc-iii.git] / drivers / nfc / s3fwrn5 / nci.c
blob075e4e877b3316ee3e9f9c45c73c8c1cb355ed9f
1 /*
2 * NCI based driver for Samsung S3FWRN5 NFC chip
4 * Copyright (C) 2015 Samsung Electrnoics
5 * Robert Baldyga <r.baldyga@samsung.com>
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms and conditions of the GNU General Public License,
9 * version 2 or later, as published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
20 #include <linux/completion.h>
21 #include <linux/firmware.h>
23 #include "s3fwrn5.h"
24 #include "nci.h"
26 static int s3fwrn5_nci_prop_rsp(struct nci_dev *ndev, struct sk_buff *skb)
28 __u8 status = skb->data[0];
30 nci_req_complete(ndev, status);
31 return 0;
34 static struct nci_driver_ops s3fwrn5_nci_prop_ops[] = {
36 .opcode = nci_opcode_pack(NCI_GID_PROPRIETARY,
37 NCI_PROP_AGAIN),
38 .rsp = s3fwrn5_nci_prop_rsp,
41 .opcode = nci_opcode_pack(NCI_GID_PROPRIETARY,
42 NCI_PROP_GET_RFREG),
43 .rsp = s3fwrn5_nci_prop_rsp,
46 .opcode = nci_opcode_pack(NCI_GID_PROPRIETARY,
47 NCI_PROP_SET_RFREG),
48 .rsp = s3fwrn5_nci_prop_rsp,
51 .opcode = nci_opcode_pack(NCI_GID_PROPRIETARY,
52 NCI_PROP_GET_RFREG_VER),
53 .rsp = s3fwrn5_nci_prop_rsp,
56 .opcode = nci_opcode_pack(NCI_GID_PROPRIETARY,
57 NCI_PROP_SET_RFREG_VER),
58 .rsp = s3fwrn5_nci_prop_rsp,
61 .opcode = nci_opcode_pack(NCI_GID_PROPRIETARY,
62 NCI_PROP_START_RFREG),
63 .rsp = s3fwrn5_nci_prop_rsp,
66 .opcode = nci_opcode_pack(NCI_GID_PROPRIETARY,
67 NCI_PROP_STOP_RFREG),
68 .rsp = s3fwrn5_nci_prop_rsp,
71 .opcode = nci_opcode_pack(NCI_GID_PROPRIETARY,
72 NCI_PROP_FW_CFG),
73 .rsp = s3fwrn5_nci_prop_rsp,
76 .opcode = nci_opcode_pack(NCI_GID_PROPRIETARY,
77 NCI_PROP_WR_RESET),
78 .rsp = s3fwrn5_nci_prop_rsp,
82 void s3fwrn5_nci_get_prop_ops(struct nci_driver_ops **ops, size_t *n)
84 *ops = s3fwrn5_nci_prop_ops;
85 *n = ARRAY_SIZE(s3fwrn5_nci_prop_ops);
88 #define S3FWRN5_RFREG_SECTION_SIZE 252
90 int s3fwrn5_nci_rf_configure(struct s3fwrn5_info *info, const char *fw_name)
92 const struct firmware *fw;
93 struct nci_prop_fw_cfg_cmd fw_cfg;
94 struct nci_prop_set_rfreg_cmd set_rfreg;
95 struct nci_prop_stop_rfreg_cmd stop_rfreg;
96 u32 checksum;
97 int i, len;
98 int ret;
100 ret = request_firmware(&fw, fw_name, &info->ndev->nfc_dev->dev);
101 if (ret < 0)
102 return ret;
104 /* Compute rfreg checksum */
106 checksum = 0;
107 for (i = 0; i < fw->size; i += 4)
108 checksum += *((u32 *)(fw->data+i));
110 /* Set default clock configuration for external crystal */
112 fw_cfg.clk_type = 0x01;
113 fw_cfg.clk_speed = 0xff;
114 fw_cfg.clk_req = 0xff;
115 ret = nci_prop_cmd(info->ndev, NCI_PROP_FW_CFG,
116 sizeof(fw_cfg), (__u8 *)&fw_cfg);
117 if (ret < 0)
118 goto out;
120 /* Start rfreg configuration */
122 dev_info(&info->ndev->nfc_dev->dev,
123 "rfreg configuration update: %s\n", fw_name);
125 ret = nci_prop_cmd(info->ndev, NCI_PROP_START_RFREG, 0, NULL);
126 if (ret < 0) {
127 dev_err(&info->ndev->nfc_dev->dev,
128 "Unable to start rfreg update\n");
129 goto out;
132 /* Update rfreg */
134 set_rfreg.index = 0;
135 for (i = 0; i < fw->size; i += S3FWRN5_RFREG_SECTION_SIZE) {
136 len = (fw->size - i < S3FWRN5_RFREG_SECTION_SIZE) ?
137 (fw->size - i) : S3FWRN5_RFREG_SECTION_SIZE;
138 memcpy(set_rfreg.data, fw->data+i, len);
139 ret = nci_prop_cmd(info->ndev, NCI_PROP_SET_RFREG,
140 len+1, (__u8 *)&set_rfreg);
141 if (ret < 0) {
142 dev_err(&info->ndev->nfc_dev->dev,
143 "rfreg update error (code=%d)\n", ret);
144 goto out;
146 set_rfreg.index++;
149 /* Finish rfreg configuration */
151 stop_rfreg.checksum = checksum & 0xffff;
152 ret = nci_prop_cmd(info->ndev, NCI_PROP_STOP_RFREG,
153 sizeof(stop_rfreg), (__u8 *)&stop_rfreg);
154 if (ret < 0) {
155 dev_err(&info->ndev->nfc_dev->dev,
156 "Unable to stop rfreg update\n");
157 goto out;
160 dev_info(&info->ndev->nfc_dev->dev,
161 "rfreg configuration update: success\n");
162 out:
163 release_firmware(fw);
164 return ret;