extcon.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368
  1. /*
  2. * drivers/extcon/extcon.c - External Connector (extcon) framework.
  3. *
  4. * External connector (extcon) class driver
  5. *
  6. * Copyright (C) 2015 Samsung Electronics
  7. * Author: Chanwoo Choi <cw00.choi@samsung.com>
  8. *
  9. * Copyright (C) 2012 Samsung Electronics
  10. * Author: Donggeun Kim <dg77.kim@samsung.com>
  11. * Author: MyungJoo Ham <myungjoo.ham@samsung.com>
  12. *
  13. * based on android/drivers/switch/switch_class.c
  14. * Copyright (C) 2008 Google, Inc.
  15. * Author: Mike Lockwood <lockwood@android.com>
  16. *
  17. * This software is licensed under the terms of the GNU General Public
  18. * License version 2, as published by the Free Software Foundation, and
  19. * may be copied, distributed, and modified under those terms.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU General Public License for more details.
  25. */
  26. #include <linux/module.h>
  27. #include <linux/types.h>
  28. #include <linux/init.h>
  29. #include <linux/device.h>
  30. #include <linux/fs.h>
  31. #include <linux/err.h>
  32. #include <linux/extcon.h>
  33. #include <linux/of.h>
  34. #include <linux/slab.h>
  35. #include <linux/sysfs.h>
  36. #define SUPPORTED_CABLE_MAX 32
  37. #define CABLE_NAME_MAX 30
  38. struct __extcon_info {
  39. unsigned int type;
  40. unsigned int id;
  41. const char *name;
  42. } extcon_info[] = {
  43. [EXTCON_NONE] = {
  44. .type = EXTCON_TYPE_MISC,
  45. .id = EXTCON_NONE,
  46. .name = "NONE",
  47. },
  48. /* USB external connector */
  49. [EXTCON_USB] = {
  50. .type = EXTCON_TYPE_USB,
  51. .id = EXTCON_USB,
  52. .name = "USB",
  53. },
  54. [EXTCON_USB_HOST] = {
  55. .type = EXTCON_TYPE_USB,
  56. .id = EXTCON_USB_HOST,
  57. .name = "USB_HOST",
  58. },
  59. /* Charging external connector */
  60. [EXTCON_CHG_USB_SDP] = {
  61. .type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB,
  62. .id = EXTCON_CHG_USB_SDP,
  63. .name = "SDP",
  64. },
  65. [EXTCON_CHG_USB_DCP] = {
  66. .type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB,
  67. .id = EXTCON_CHG_USB_DCP,
  68. .name = "DCP",
  69. },
  70. [EXTCON_CHG_USB_CDP] = {
  71. .type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB,
  72. .id = EXTCON_CHG_USB_CDP,
  73. .name = "CDP",
  74. },
  75. [EXTCON_CHG_USB_ACA] = {
  76. .type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB,
  77. .id = EXTCON_CHG_USB_ACA,
  78. .name = "ACA",
  79. },
  80. [EXTCON_CHG_USB_FAST] = {
  81. .type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB,
  82. .id = EXTCON_CHG_USB_FAST,
  83. .name = "FAST-CHARGER",
  84. },
  85. [EXTCON_CHG_USB_SLOW] = {
  86. .type = EXTCON_TYPE_CHG | EXTCON_TYPE_USB,
  87. .id = EXTCON_CHG_USB_SLOW,
  88. .name = "SLOW-CHARGER",
  89. },
  90. [EXTCON_CHG_WPT] = {
  91. .type = EXTCON_TYPE_CHG,
  92. .id = EXTCON_CHG_WPT,
  93. .name = "WPT",
  94. },
  95. /* Jack external connector */
  96. [EXTCON_JACK_MICROPHONE] = {
  97. .type = EXTCON_TYPE_JACK,
  98. .id = EXTCON_JACK_MICROPHONE,
  99. .name = "MICROPHONE",
  100. },
  101. [EXTCON_JACK_HEADPHONE] = {
  102. .type = EXTCON_TYPE_JACK,
  103. .id = EXTCON_JACK_HEADPHONE,
  104. .name = "HEADPHONE",
  105. },
  106. [EXTCON_JACK_LINE_IN] = {
  107. .type = EXTCON_TYPE_JACK,
  108. .id = EXTCON_JACK_LINE_IN,
  109. .name = "LINE-IN",
  110. },
  111. [EXTCON_JACK_LINE_OUT] = {
  112. .type = EXTCON_TYPE_JACK,
  113. .id = EXTCON_JACK_LINE_OUT,
  114. .name = "LINE-OUT",
  115. },
  116. [EXTCON_JACK_VIDEO_IN] = {
  117. .type = EXTCON_TYPE_JACK,
  118. .id = EXTCON_JACK_VIDEO_IN,
  119. .name = "VIDEO-IN",
  120. },
  121. [EXTCON_JACK_VIDEO_OUT] = {
  122. .type = EXTCON_TYPE_JACK,
  123. .id = EXTCON_JACK_VIDEO_OUT,
  124. .name = "VIDEO-OUT",
  125. },
  126. [EXTCON_JACK_SPDIF_IN] = {
  127. .type = EXTCON_TYPE_JACK,
  128. .id = EXTCON_JACK_SPDIF_IN,
  129. .name = "SPDIF-IN",
  130. },
  131. [EXTCON_JACK_SPDIF_OUT] = {
  132. .type = EXTCON_TYPE_JACK,
  133. .id = EXTCON_JACK_SPDIF_OUT,
  134. .name = "SPDIF-OUT",
  135. },
  136. /* Display external connector */
  137. [EXTCON_DISP_HDMI] = {
  138. .type = EXTCON_TYPE_DISP,
  139. .id = EXTCON_DISP_HDMI,
  140. .name = "HDMI",
  141. },
  142. [EXTCON_DISP_MHL] = {
  143. .type = EXTCON_TYPE_DISP,
  144. .id = EXTCON_DISP_MHL,
  145. .name = "MHL",
  146. },
  147. [EXTCON_DISP_DVI] = {
  148. .type = EXTCON_TYPE_DISP,
  149. .id = EXTCON_DISP_DVI,
  150. .name = "DVI",
  151. },
  152. [EXTCON_DISP_VGA] = {
  153. .type = EXTCON_TYPE_DISP,
  154. .id = EXTCON_DISP_VGA,
  155. .name = "VGA",
  156. },
  157. [EXTCON_DISP_DP] = {
  158. .type = EXTCON_TYPE_DISP | EXTCON_TYPE_USB,
  159. .id = EXTCON_DISP_DP,
  160. .name = "DP",
  161. },
  162. [EXTCON_DISP_HMD] = {
  163. .type = EXTCON_TYPE_DISP | EXTCON_TYPE_USB,
  164. .id = EXTCON_DISP_HMD,
  165. .name = "HMD",
  166. },
  167. /* Miscellaneous external connector */
  168. [EXTCON_DOCK] = {
  169. .type = EXTCON_TYPE_MISC,
  170. .id = EXTCON_DOCK,
  171. .name = "DOCK",
  172. },
  173. [EXTCON_JIG] = {
  174. .type = EXTCON_TYPE_MISC,
  175. .id = EXTCON_JIG,
  176. .name = "JIG",
  177. },
  178. [EXTCON_MECHANICAL] = {
  179. .type = EXTCON_TYPE_MISC,
  180. .id = EXTCON_MECHANICAL,
  181. .name = "MECHANICAL",
  182. },
  183. { /* sentinel */ }
  184. };
  185. /**
  186. * struct extcon_cable - An internal data for each cable of extcon device.
  187. * @edev: The extcon device
  188. * @cable_index: Index of this cable in the edev
  189. * @attr_g: Attribute group for the cable
  190. * @attr_name: "name" sysfs entry
  191. * @attr_state: "state" sysfs entry
  192. * @attrs: Array pointing to attr_name and attr_state for attr_g
  193. */
  194. struct extcon_cable {
  195. struct extcon_dev *edev;
  196. int cable_index;
  197. struct attribute_group attr_g;
  198. struct device_attribute attr_name;
  199. struct device_attribute attr_state;
  200. struct attribute *attrs[3]; /* to be fed to attr_g.attrs */
  201. union extcon_property_value usb_propval[EXTCON_PROP_USB_CNT];
  202. union extcon_property_value chg_propval[EXTCON_PROP_CHG_CNT];
  203. union extcon_property_value jack_propval[EXTCON_PROP_JACK_CNT];
  204. union extcon_property_value disp_propval[EXTCON_PROP_DISP_CNT];
  205. unsigned long usb_bits[BITS_TO_LONGS(EXTCON_PROP_USB_CNT)];
  206. unsigned long chg_bits[BITS_TO_LONGS(EXTCON_PROP_CHG_CNT)];
  207. unsigned long jack_bits[BITS_TO_LONGS(EXTCON_PROP_JACK_CNT)];
  208. unsigned long disp_bits[BITS_TO_LONGS(EXTCON_PROP_DISP_CNT)];
  209. };
  210. static struct class *extcon_class;
  211. #if defined(CONFIG_ANDROID)
  212. static struct class_compat *switch_class;
  213. #endif /* CONFIG_ANDROID */
  214. static LIST_HEAD(extcon_dev_list);
  215. static DEFINE_MUTEX(extcon_dev_list_lock);
  216. /**
  217. * check_mutually_exclusive - Check if new_state violates mutually_exclusive
  218. * condition.
  219. * @edev: the extcon device
  220. * @new_state: new cable attach status for @edev
  221. *
  222. * Returns 0 if nothing violates. Returns the index + 1 for the first
  223. * violated condition.
  224. */
  225. static int check_mutually_exclusive(struct extcon_dev *edev, u32 new_state)
  226. {
  227. int i = 0;
  228. if (!edev->mutually_exclusive)
  229. return 0;
  230. for (i = 0; edev->mutually_exclusive[i]; i++) {
  231. int weight;
  232. u32 correspondants = new_state & edev->mutually_exclusive[i];
  233. /* calculate the total number of bits set */
  234. weight = hweight32(correspondants);
  235. if (weight > 1)
  236. return i + 1;
  237. }
  238. return 0;
  239. }
  240. static int find_cable_index_by_id(struct extcon_dev *edev, const unsigned int id)
  241. {
  242. int i;
  243. /* Find the the index of extcon cable in edev->supported_cable */
  244. for (i = 0; i < edev->max_supported; i++) {
  245. if (edev->supported_cable[i] == id)
  246. return i;
  247. }
  248. return -EINVAL;
  249. }
  250. static int get_extcon_type(unsigned int prop)
  251. {
  252. switch (prop) {
  253. case EXTCON_PROP_USB_MIN ... EXTCON_PROP_USB_MAX:
  254. return EXTCON_TYPE_USB;
  255. case EXTCON_PROP_CHG_MIN ... EXTCON_PROP_CHG_MAX:
  256. return EXTCON_TYPE_CHG;
  257. case EXTCON_PROP_JACK_MIN ... EXTCON_PROP_JACK_MAX:
  258. return EXTCON_TYPE_JACK;
  259. case EXTCON_PROP_DISP_MIN ... EXTCON_PROP_DISP_MAX:
  260. return EXTCON_TYPE_DISP;
  261. default:
  262. return -EINVAL;
  263. }
  264. }
  265. static bool is_extcon_attached(struct extcon_dev *edev, unsigned int index)
  266. {
  267. return !!(edev->state & BIT(index));
  268. }
  269. static bool is_extcon_changed(struct extcon_dev *edev, int index,
  270. bool new_state)
  271. {
  272. int state = !!(edev->state & BIT(index));
  273. return (state != new_state);
  274. }
  275. static bool is_extcon_property_supported(unsigned int id, unsigned int prop)
  276. {
  277. int type;
  278. /* Check whether the property is supported or not. */
  279. type = get_extcon_type(prop);
  280. if (type < 0)
  281. return false;
  282. /* Check whether a specific extcon id supports the property or not. */
  283. return !!(extcon_info[id].type & type);
  284. }
  285. static int is_extcon_property_capability(struct extcon_dev *edev,
  286. unsigned int id, int index,unsigned int prop)
  287. {
  288. struct extcon_cable *cable;
  289. int type, ret;
  290. /* Check whether the property is supported or not. */
  291. type = get_extcon_type(prop);
  292. if (type < 0)
  293. return type;
  294. cable = &edev->cables[index];
  295. switch (type) {
  296. case EXTCON_TYPE_USB:
  297. ret = test_bit(prop - EXTCON_PROP_USB_MIN, cable->usb_bits);
  298. break;
  299. case EXTCON_TYPE_CHG:
  300. ret = test_bit(prop - EXTCON_PROP_CHG_MIN, cable->chg_bits);
  301. break;
  302. case EXTCON_TYPE_JACK:
  303. ret = test_bit(prop - EXTCON_PROP_JACK_MIN, cable->jack_bits);
  304. break;
  305. case EXTCON_TYPE_DISP:
  306. ret = test_bit(prop - EXTCON_PROP_DISP_MIN, cable->disp_bits);
  307. break;
  308. default:
  309. ret = -EINVAL;
  310. }
  311. return ret;
  312. }
  313. static void init_property(struct extcon_dev *edev, unsigned int id, int index)
  314. {
  315. unsigned int type = extcon_info[id].type;
  316. struct extcon_cable *cable = &edev->cables[index];
  317. if (EXTCON_TYPE_USB & type)
  318. memset(cable->usb_propval, 0, sizeof(cable->usb_propval));
  319. if (EXTCON_TYPE_CHG & type)
  320. memset(cable->chg_propval, 0, sizeof(cable->chg_propval));
  321. if (EXTCON_TYPE_JACK & type)
  322. memset(cable->jack_propval, 0, sizeof(cable->jack_propval));
  323. if (EXTCON_TYPE_DISP & type)
  324. memset(cable->disp_propval, 0, sizeof(cable->disp_propval));
  325. }
  326. static ssize_t state_show(struct device *dev, struct device_attribute *attr,
  327. char *buf)
  328. {
  329. int i, count = 0;
  330. struct extcon_dev *edev = dev_get_drvdata(dev);
  331. if (edev->max_supported == 0)
  332. return sprintf(buf, "%u\n", edev->state);
  333. for (i = 0; i < edev->max_supported; i++) {
  334. count += sprintf(buf + count, "%s=%d\n",
  335. extcon_info[edev->supported_cable[i]].name,
  336. !!(edev->state & (1 << i)));
  337. }
  338. return count;
  339. }
  340. static DEVICE_ATTR_RO(state);
  341. static ssize_t name_show(struct device *dev, struct device_attribute *attr,
  342. char *buf)
  343. {
  344. struct extcon_dev *edev = dev_get_drvdata(dev);
  345. return sprintf(buf, "%s\n", edev->name);
  346. }
  347. static DEVICE_ATTR_RO(name);
  348. static ssize_t cable_name_show(struct device *dev,
  349. struct device_attribute *attr, char *buf)
  350. {
  351. struct extcon_cable *cable = container_of(attr, struct extcon_cable,
  352. attr_name);
  353. int i = cable->cable_index;
  354. return sprintf(buf, "%s\n",
  355. extcon_info[cable->edev->supported_cable[i]].name);
  356. }
  357. static ssize_t cable_state_show(struct device *dev,
  358. struct device_attribute *attr, char *buf)
  359. {
  360. struct extcon_cable *cable = container_of(attr, struct extcon_cable,
  361. attr_state);
  362. int i = cable->cable_index;
  363. return sprintf(buf, "%d\n",
  364. extcon_get_state(cable->edev, cable->edev->supported_cable[i]));
  365. }
  366. /**
  367. * extcon_sync() - Synchronize the states for both the attached/detached
  368. * @edev: the extcon device that has the cable.
  369. *
  370. * This function send a notification to synchronize the all states of a
  371. * specific external connector
  372. */
  373. int extcon_sync(struct extcon_dev *edev, unsigned int id)
  374. {
  375. char name_buf[120];
  376. char state_buf[120];
  377. char *prop_buf;
  378. char *envp[3];
  379. int env_offset = 0;
  380. int length;
  381. int index;
  382. int state;
  383. unsigned long flags;
  384. if (!edev)
  385. return -EINVAL;
  386. index = find_cable_index_by_id(edev, id);
  387. if (index < 0)
  388. return index;
  389. spin_lock_irqsave(&edev->lock, flags);
  390. state = !!(edev->state & BIT(index));
  391. raw_notifier_call_chain(&edev->nh[index], state, edev);
  392. /* This could be in interrupt handler */
  393. prop_buf = (char *)get_zeroed_page(GFP_ATOMIC);
  394. if (!prop_buf) {
  395. /* Unlock early before uevent */
  396. spin_unlock_irqrestore(&edev->lock, flags);
  397. dev_err(&edev->dev, "out of memory in extcon_set_state\n");
  398. kobject_uevent(&edev->dev.kobj, KOBJ_CHANGE);
  399. return -ENOMEM;
  400. }
  401. length = name_show(&edev->dev, NULL, prop_buf);
  402. if (length > 0) {
  403. if (prop_buf[length - 1] == '\n')
  404. prop_buf[length - 1] = 0;
  405. snprintf(name_buf, sizeof(name_buf), "NAME=%s", prop_buf);
  406. envp[env_offset++] = name_buf;
  407. }
  408. length = state_show(&edev->dev, NULL, prop_buf);
  409. if (length > 0) {
  410. if (prop_buf[length - 1] == '\n')
  411. prop_buf[length - 1] = 0;
  412. snprintf(state_buf, sizeof(state_buf), "STATE=%s", prop_buf);
  413. envp[env_offset++] = state_buf;
  414. }
  415. envp[env_offset] = NULL;
  416. /* Unlock early before uevent */
  417. spin_unlock_irqrestore(&edev->lock, flags);
  418. kobject_uevent_env(&edev->dev.kobj, KOBJ_CHANGE, envp);
  419. free_page((unsigned long)prop_buf);
  420. return 0;
  421. }
  422. EXPORT_SYMBOL_GPL(extcon_sync);
  423. /**
  424. * extcon_get_state() - Get the state of a external connector.
  425. * @edev: the extcon device that has the cable.
  426. * @id: the unique id of each external connector in extcon enumeration.
  427. */
  428. int extcon_get_state(struct extcon_dev *edev, const unsigned int id)
  429. {
  430. int index, state;
  431. unsigned long flags;
  432. if (!edev)
  433. return -EINVAL;
  434. index = find_cable_index_by_id(edev, id);
  435. if (index < 0)
  436. return index;
  437. spin_lock_irqsave(&edev->lock, flags);
  438. state = is_extcon_attached(edev, index);
  439. spin_unlock_irqrestore(&edev->lock, flags);
  440. return state;
  441. }
  442. EXPORT_SYMBOL_GPL(extcon_get_state);
  443. /**
  444. * extcon_set_state() - Set the state of a external connector.
  445. * without a notification.
  446. * @edev: the extcon device that has the cable.
  447. * @id: the unique id of each external connector
  448. * in extcon enumeration.
  449. * @state: the new cable status. The default semantics is
  450. * true: attached / false: detached.
  451. *
  452. * This function only set the state of a external connector without
  453. * a notification. To synchronize the data of a external connector,
  454. * use extcon_set_state_sync() and extcon_sync().
  455. */
  456. int extcon_set_state(struct extcon_dev *edev, unsigned int id,
  457. bool cable_state)
  458. {
  459. unsigned long flags;
  460. int index, ret = 0;
  461. if (!edev)
  462. return -EINVAL;
  463. index = find_cable_index_by_id(edev, id);
  464. if (index < 0)
  465. return index;
  466. spin_lock_irqsave(&edev->lock, flags);
  467. /* Check whether the external connector's state is changed. */
  468. if (!is_extcon_changed(edev, index, cable_state))
  469. goto out;
  470. if (check_mutually_exclusive(edev,
  471. (edev->state & ~BIT(index)) | (cable_state & BIT(index)))) {
  472. ret = -EPERM;
  473. goto out;
  474. }
  475. /*
  476. * Initialize the value of extcon property before setting
  477. * the detached state for an external connector.
  478. */
  479. if (!cable_state)
  480. init_property(edev, id, index);
  481. /* Update the state for a external connector. */
  482. if (cable_state)
  483. edev->state |= BIT(index);
  484. else
  485. edev->state &= ~(BIT(index));
  486. out:
  487. spin_unlock_irqrestore(&edev->lock, flags);
  488. return ret;
  489. }
  490. EXPORT_SYMBOL_GPL(extcon_set_state);
  491. /**
  492. * extcon_set_state_sync() - Set the state of a external connector
  493. * with a notification.
  494. * @edev: the extcon device that has the cable.
  495. * @id: the unique id of each external connector
  496. * in extcon enumeration.
  497. * @state: the new cable status. The default semantics is
  498. * true: attached / false: detached.
  499. *
  500. * This function set the state of external connector and synchronize the data
  501. * by usning a notification.
  502. */
  503. int extcon_set_state_sync(struct extcon_dev *edev, unsigned int id,
  504. bool cable_state)
  505. {
  506. int ret, index;
  507. unsigned long flags;
  508. index = find_cable_index_by_id(edev, id);
  509. if (index < 0)
  510. return index;
  511. /* Check whether the external connector's state is changed. */
  512. spin_lock_irqsave(&edev->lock, flags);
  513. ret = is_extcon_changed(edev, index, cable_state);
  514. spin_unlock_irqrestore(&edev->lock, flags);
  515. if (!ret)
  516. return 0;
  517. ret = extcon_set_state(edev, id, cable_state);
  518. if (ret < 0)
  519. return ret;
  520. return extcon_sync(edev, id);
  521. }
  522. EXPORT_SYMBOL_GPL(extcon_set_state_sync);
  523. /**
  524. * extcon_get_property() - Get the property value of a specific cable.
  525. * @edev: the extcon device that has the cable.
  526. * @id: the unique id of each external connector
  527. * in extcon enumeration.
  528. * @prop: the property id among enum extcon_property.
  529. * @prop_val: the pointer which store the value of property.
  530. *
  531. * When getting the property value of external connector, the external connector
  532. * should be attached. If detached state, function just return 0 without
  533. * property value. Also, the each property should be included in the list of
  534. * supported properties according to the type of external connectors.
  535. *
  536. * Returns 0 if success or error number if fail
  537. */
  538. int extcon_get_property(struct extcon_dev *edev, unsigned int id,
  539. unsigned int prop,
  540. union extcon_property_value *prop_val)
  541. {
  542. struct extcon_cable *cable;
  543. unsigned long flags;
  544. int index, ret = 0;
  545. *prop_val = (union extcon_property_value)(0);
  546. if (!edev)
  547. return -EINVAL;
  548. /* Check whether the property is supported or not */
  549. if (!is_extcon_property_supported(id, prop))
  550. return -EINVAL;
  551. /* Find the cable index of external connector by using id */
  552. index = find_cable_index_by_id(edev, id);
  553. if (index < 0)
  554. return index;
  555. spin_lock_irqsave(&edev->lock, flags);
  556. /* Check whether the property is available or not. */
  557. if (!is_extcon_property_capability(edev, id, index, prop)) {
  558. spin_unlock_irqrestore(&edev->lock, flags);
  559. return -EPERM;
  560. }
  561. /*
  562. * Check whether the external connector is attached.
  563. * If external connector is detached, the user can not
  564. * get the property value.
  565. */
  566. if (!is_extcon_attached(edev, index)) {
  567. spin_unlock_irqrestore(&edev->lock, flags);
  568. return 0;
  569. }
  570. cable = &edev->cables[index];
  571. /* Get the property value according to extcon type */
  572. switch (prop) {
  573. case EXTCON_PROP_USB_MIN ... EXTCON_PROP_USB_MAX:
  574. *prop_val = cable->usb_propval[prop - EXTCON_PROP_USB_MIN];
  575. break;
  576. case EXTCON_PROP_CHG_MIN ... EXTCON_PROP_CHG_MAX:
  577. *prop_val = cable->chg_propval[prop - EXTCON_PROP_CHG_MIN];
  578. break;
  579. case EXTCON_PROP_JACK_MIN ... EXTCON_PROP_JACK_MAX:
  580. *prop_val = cable->jack_propval[prop - EXTCON_PROP_JACK_MIN];
  581. break;
  582. case EXTCON_PROP_DISP_MIN ... EXTCON_PROP_DISP_MAX:
  583. *prop_val = cable->disp_propval[prop - EXTCON_PROP_DISP_MIN];
  584. break;
  585. default:
  586. ret = -EINVAL;
  587. break;
  588. }
  589. spin_unlock_irqrestore(&edev->lock, flags);
  590. return ret;
  591. }
  592. EXPORT_SYMBOL_GPL(extcon_get_property);
  593. /**
  594. * extcon_set_property() - Set the property value of a specific cable.
  595. * @edev: the extcon device that has the cable.
  596. * @id: the unique id of each external connector
  597. * in extcon enumeration.
  598. * @prop: the property id among enum extcon_property.
  599. * @prop_val: the pointer including the new value of property.
  600. *
  601. * The each property should be included in the list of supported properties
  602. * according to the type of external connectors.
  603. *
  604. * Returns 0 if success or error number if fail
  605. */
  606. int extcon_set_property(struct extcon_dev *edev, unsigned int id,
  607. unsigned int prop,
  608. union extcon_property_value prop_val)
  609. {
  610. struct extcon_cable *cable;
  611. unsigned long flags;
  612. int index, ret = 0;
  613. if (!edev)
  614. return -EINVAL;
  615. /* Check whether the property is supported or not */
  616. if (!is_extcon_property_supported(id, prop))
  617. return -EINVAL;
  618. /* Find the cable index of external connector by using id */
  619. index = find_cable_index_by_id(edev, id);
  620. if (index < 0)
  621. return index;
  622. spin_lock_irqsave(&edev->lock, flags);
  623. /* Check whether the property is available or not. */
  624. if (!is_extcon_property_capability(edev, id, index, prop)) {
  625. spin_unlock_irqrestore(&edev->lock, flags);
  626. return -EPERM;
  627. }
  628. cable = &edev->cables[index];
  629. /* Set the property value according to extcon type */
  630. switch (prop) {
  631. case EXTCON_PROP_USB_MIN ... EXTCON_PROP_USB_MAX:
  632. cable->usb_propval[prop - EXTCON_PROP_USB_MIN] = prop_val;
  633. break;
  634. case EXTCON_PROP_CHG_MIN ... EXTCON_PROP_CHG_MAX:
  635. cable->chg_propval[prop - EXTCON_PROP_CHG_MIN] = prop_val;
  636. break;
  637. case EXTCON_PROP_JACK_MIN ... EXTCON_PROP_JACK_MAX:
  638. cable->jack_propval[prop - EXTCON_PROP_JACK_MIN] = prop_val;
  639. break;
  640. case EXTCON_PROP_DISP_MIN ... EXTCON_PROP_DISP_MAX:
  641. cable->disp_propval[prop - EXTCON_PROP_DISP_MIN] = prop_val;
  642. break;
  643. default:
  644. ret = -EINVAL;
  645. break;
  646. }
  647. spin_unlock_irqrestore(&edev->lock, flags);
  648. return ret;
  649. }
  650. EXPORT_SYMBOL_GPL(extcon_set_property);
  651. /**
  652. * extcon_set_property_sync() - Set the property value of a specific cable
  653. with a notification.
  654. * @prop_val: the pointer including the new value of property.
  655. *
  656. * When setting the property value of external connector, the external connector
  657. * should be attached. The each property should be included in the list of
  658. * supported properties according to the type of external connectors.
  659. *
  660. * Returns 0 if success or error number if fail
  661. */
  662. int extcon_set_property_sync(struct extcon_dev *edev, unsigned int id,
  663. unsigned int prop,
  664. union extcon_property_value prop_val)
  665. {
  666. int ret;
  667. ret = extcon_set_property(edev, id, prop, prop_val);
  668. if (ret < 0)
  669. return ret;
  670. return extcon_sync(edev, id);
  671. }
  672. EXPORT_SYMBOL_GPL(extcon_set_property_sync);
  673. /**
  674. * extcon_get_property_capability() - Get the capability of property
  675. * of an external connector.
  676. * @edev: the extcon device that has the cable.
  677. * @id: the unique id of each external connector
  678. * in extcon enumeration.
  679. * @prop: the property id among enum extcon_property.
  680. *
  681. * Returns 1 if the property is available or 0 if not available.
  682. */
  683. int extcon_get_property_capability(struct extcon_dev *edev, unsigned int id,
  684. unsigned int prop)
  685. {
  686. int index;
  687. if (!edev)
  688. return -EINVAL;
  689. /* Check whether the property is supported or not */
  690. if (!is_extcon_property_supported(id, prop))
  691. return -EINVAL;
  692. /* Find the cable index of external connector by using id */
  693. index = find_cable_index_by_id(edev, id);
  694. if (index < 0)
  695. return index;
  696. return is_extcon_property_capability(edev, id, index, prop);
  697. }
  698. EXPORT_SYMBOL_GPL(extcon_get_property_capability);
  699. /**
  700. * extcon_set_property_capability() - Set the capability of a property
  701. * of an external connector.
  702. * @edev: the extcon device that has the cable.
  703. * @id: the unique id of each external connector
  704. * in extcon enumeration.
  705. * @prop: the property id among enum extcon_property.
  706. *
  707. * This function set the capability of a property for an external connector
  708. * to mark the bit in capability bitmap which mean the available state of
  709. * a property.
  710. *
  711. * Returns 0 if success or error number if fail
  712. */
  713. int extcon_set_property_capability(struct extcon_dev *edev, unsigned int id,
  714. unsigned int prop)
  715. {
  716. struct extcon_cable *cable;
  717. int index, type, ret = 0;
  718. if (!edev)
  719. return -EINVAL;
  720. /* Check whether the property is supported or not. */
  721. if (!is_extcon_property_supported(id, prop))
  722. return -EINVAL;
  723. /* Find the cable index of external connector by using id. */
  724. index = find_cable_index_by_id(edev, id);
  725. if (index < 0)
  726. return index;
  727. type = get_extcon_type(prop);
  728. if (type < 0)
  729. return type;
  730. cable = &edev->cables[index];
  731. switch (type) {
  732. case EXTCON_TYPE_USB:
  733. __set_bit(prop - EXTCON_PROP_USB_MIN, cable->usb_bits);
  734. break;
  735. case EXTCON_TYPE_CHG:
  736. __set_bit(prop - EXTCON_PROP_CHG_MIN, cable->chg_bits);
  737. break;
  738. case EXTCON_TYPE_JACK:
  739. __set_bit(prop - EXTCON_PROP_JACK_MIN, cable->jack_bits);
  740. break;
  741. case EXTCON_TYPE_DISP:
  742. __set_bit(prop - EXTCON_PROP_DISP_MIN, cable->disp_bits);
  743. break;
  744. default:
  745. ret = -EINVAL;
  746. }
  747. return ret;
  748. }
  749. EXPORT_SYMBOL_GPL(extcon_set_property_capability);
  750. /**
  751. * extcon_get_extcon_dev() - Get the extcon device instance from the name
  752. * @extcon_name: The extcon name provided with extcon_dev_register()
  753. */
  754. struct extcon_dev *extcon_get_extcon_dev(const char *extcon_name)
  755. {
  756. struct extcon_dev *sd;
  757. if (!extcon_name)
  758. return ERR_PTR(-EINVAL);
  759. mutex_lock(&extcon_dev_list_lock);
  760. list_for_each_entry(sd, &extcon_dev_list, entry) {
  761. if (!strcmp(sd->name, extcon_name))
  762. goto out;
  763. }
  764. sd = NULL;
  765. out:
  766. mutex_unlock(&extcon_dev_list_lock);
  767. return sd;
  768. }
  769. EXPORT_SYMBOL_GPL(extcon_get_extcon_dev);
  770. /**
  771. * extcon_register_notifier() - Register a notifiee to get notified by
  772. * any attach status changes from the extcon.
  773. * @edev: the extcon device that has the external connecotr.
  774. * @id: the unique id of each external connector in extcon enumeration.
  775. * @nb: a notifier block to be registered.
  776. *
  777. * Note that the second parameter given to the callback of nb (val) is
  778. * "old_state", not the current state. The current state can be retrieved
  779. * by looking at the third pameter (edev pointer)'s state value.
  780. */
  781. int extcon_register_notifier(struct extcon_dev *edev, unsigned int id,
  782. struct notifier_block *nb)
  783. {
  784. unsigned long flags;
  785. int ret, idx = -EINVAL;
  786. if (!edev || !nb)
  787. return -EINVAL;
  788. idx = find_cable_index_by_id(edev, id);
  789. if (idx < 0)
  790. return idx;
  791. spin_lock_irqsave(&edev->lock, flags);
  792. ret = raw_notifier_chain_register(&edev->nh[idx], nb);
  793. spin_unlock_irqrestore(&edev->lock, flags);
  794. return ret;
  795. }
  796. EXPORT_SYMBOL_GPL(extcon_register_notifier);
  797. /**
  798. * extcon_unregister_notifier() - Unregister a notifiee from the extcon device.
  799. * @edev: the extcon device that has the external connecotr.
  800. * @id: the unique id of each external connector in extcon enumeration.
  801. * @nb: a notifier block to be registered.
  802. */
  803. int extcon_unregister_notifier(struct extcon_dev *edev, unsigned int id,
  804. struct notifier_block *nb)
  805. {
  806. unsigned long flags;
  807. int ret, idx;
  808. if (!edev || !nb)
  809. return -EINVAL;
  810. idx = find_cable_index_by_id(edev, id);
  811. if (idx < 0)
  812. return idx;
  813. spin_lock_irqsave(&edev->lock, flags);
  814. ret = raw_notifier_chain_unregister(&edev->nh[idx], nb);
  815. spin_unlock_irqrestore(&edev->lock, flags);
  816. return ret;
  817. }
  818. EXPORT_SYMBOL_GPL(extcon_unregister_notifier);
  819. static struct attribute *extcon_attrs[] = {
  820. &dev_attr_state.attr,
  821. &dev_attr_name.attr,
  822. NULL,
  823. };
  824. ATTRIBUTE_GROUPS(extcon);
  825. static int create_extcon_class(void)
  826. {
  827. if (!extcon_class) {
  828. extcon_class = class_create(THIS_MODULE, "extcon");
  829. if (IS_ERR(extcon_class))
  830. return PTR_ERR(extcon_class);
  831. extcon_class->dev_groups = extcon_groups;
  832. #if defined(CONFIG_ANDROID)
  833. switch_class = class_compat_register("switch");
  834. if (WARN(!switch_class, "cannot allocate"))
  835. return -ENOMEM;
  836. #endif /* CONFIG_ANDROID */
  837. }
  838. return 0;
  839. }
  840. static void extcon_dev_release(struct device *dev)
  841. {
  842. }
  843. static const char *muex_name = "mutually_exclusive";
  844. static void dummy_sysfs_dev_release(struct device *dev)
  845. {
  846. }
  847. /*
  848. * extcon_dev_allocate() - Allocate the memory of extcon device.
  849. * @supported_cable: Array of supported extcon ending with EXTCON_NONE.
  850. * If supported_cable is NULL, cable name related APIs
  851. * are disabled.
  852. *
  853. * This function allocates the memory for extcon device without allocating
  854. * memory in each extcon provider driver and initialize default setting for
  855. * extcon device.
  856. *
  857. * Return the pointer of extcon device if success or ERR_PTR(err) if fail
  858. */
  859. struct extcon_dev *extcon_dev_allocate(const unsigned int *supported_cable)
  860. {
  861. struct extcon_dev *edev;
  862. if (!supported_cable)
  863. return ERR_PTR(-EINVAL);
  864. edev = kzalloc(sizeof(*edev), GFP_KERNEL);
  865. if (!edev)
  866. return ERR_PTR(-ENOMEM);
  867. edev->max_supported = 0;
  868. edev->supported_cable = supported_cable;
  869. return edev;
  870. }
  871. /*
  872. * extcon_dev_free() - Free the memory of extcon device.
  873. * @edev: the extcon device to free
  874. */
  875. void extcon_dev_free(struct extcon_dev *edev)
  876. {
  877. kfree(edev);
  878. }
  879. EXPORT_SYMBOL_GPL(extcon_dev_free);
  880. /**
  881. * extcon_dev_register() - Register a new extcon device
  882. * @edev : the new extcon device (should be allocated before calling)
  883. *
  884. * Among the members of edev struct, please set the "user initializing data"
  885. * in any case and set the "optional callbacks" if required. However, please
  886. * do not set the values of "internal data", which are initialized by
  887. * this function.
  888. */
  889. int extcon_dev_register(struct extcon_dev *edev)
  890. {
  891. int ret, index = 0;
  892. static atomic_t edev_no = ATOMIC_INIT(-1);
  893. if (!extcon_class) {
  894. ret = create_extcon_class();
  895. if (ret < 0)
  896. return ret;
  897. }
  898. if (!edev || !edev->supported_cable)
  899. return -EINVAL;
  900. for (; edev->supported_cable[index] != EXTCON_NONE; index++);
  901. edev->max_supported = index;
  902. if (index > SUPPORTED_CABLE_MAX) {
  903. dev_err(&edev->dev,
  904. "exceed the maximum number of supported cables\n");
  905. return -EINVAL;
  906. }
  907. edev->dev.class = extcon_class;
  908. edev->dev.release = extcon_dev_release;
  909. edev->name = dev_name(edev->dev.parent);
  910. if (IS_ERR_OR_NULL(edev->name)) {
  911. dev_err(&edev->dev,
  912. "extcon device name is null\n");
  913. return -EINVAL;
  914. }
  915. dev_set_name(&edev->dev, "extcon%lu",
  916. (unsigned long)atomic_inc_return(&edev_no));
  917. if (edev->max_supported) {
  918. char buf[10];
  919. char *str;
  920. struct extcon_cable *cable;
  921. edev->cables = kzalloc(sizeof(struct extcon_cable) *
  922. edev->max_supported, GFP_KERNEL);
  923. if (!edev->cables) {
  924. ret = -ENOMEM;
  925. goto err_sysfs_alloc;
  926. }
  927. for (index = 0; index < edev->max_supported; index++) {
  928. cable = &edev->cables[index];
  929. snprintf(buf, 10, "cable.%d", index);
  930. str = kzalloc(sizeof(char) * (strlen(buf) + 1),
  931. GFP_KERNEL);
  932. if (!str) {
  933. for (index--; index >= 0; index--) {
  934. cable = &edev->cables[index];
  935. kfree(cable->attr_g.name);
  936. }
  937. ret = -ENOMEM;
  938. goto err_alloc_cables;
  939. }
  940. strcpy(str, buf);
  941. cable->edev = edev;
  942. cable->cable_index = index;
  943. cable->attrs[0] = &cable->attr_name.attr;
  944. cable->attrs[1] = &cable->attr_state.attr;
  945. cable->attrs[2] = NULL;
  946. cable->attr_g.name = str;
  947. cable->attr_g.attrs = cable->attrs;
  948. sysfs_attr_init(&cable->attr_name.attr);
  949. cable->attr_name.attr.name = "name";
  950. cable->attr_name.attr.mode = 0444;
  951. cable->attr_name.show = cable_name_show;
  952. sysfs_attr_init(&cable->attr_state.attr);
  953. cable->attr_state.attr.name = "state";
  954. cable->attr_state.attr.mode = 0444;
  955. cable->attr_state.show = cable_state_show;
  956. }
  957. }
  958. if (edev->max_supported && edev->mutually_exclusive) {
  959. char buf[80];
  960. char *name;
  961. /* Count the size of mutually_exclusive array */
  962. for (index = 0; edev->mutually_exclusive[index]; index++)
  963. ;
  964. edev->attrs_muex = kzalloc(sizeof(struct attribute *) *
  965. (index + 1), GFP_KERNEL);
  966. if (!edev->attrs_muex) {
  967. ret = -ENOMEM;
  968. goto err_muex;
  969. }
  970. edev->d_attrs_muex = kzalloc(sizeof(struct device_attribute) *
  971. index, GFP_KERNEL);
  972. if (!edev->d_attrs_muex) {
  973. ret = -ENOMEM;
  974. kfree(edev->attrs_muex);
  975. goto err_muex;
  976. }
  977. for (index = 0; edev->mutually_exclusive[index]; index++) {
  978. sprintf(buf, "0x%x", edev->mutually_exclusive[index]);
  979. name = kzalloc(sizeof(char) * (strlen(buf) + 1),
  980. GFP_KERNEL);
  981. if (!name) {
  982. for (index--; index >= 0; index--) {
  983. kfree(edev->d_attrs_muex[index].attr.
  984. name);
  985. }
  986. kfree(edev->d_attrs_muex);
  987. kfree(edev->attrs_muex);
  988. ret = -ENOMEM;
  989. goto err_muex;
  990. }
  991. strcpy(name, buf);
  992. sysfs_attr_init(&edev->d_attrs_muex[index].attr);
  993. edev->d_attrs_muex[index].attr.name = name;
  994. edev->d_attrs_muex[index].attr.mode = 0000;
  995. edev->attrs_muex[index] = &edev->d_attrs_muex[index]
  996. .attr;
  997. }
  998. edev->attr_g_muex.name = muex_name;
  999. edev->attr_g_muex.attrs = edev->attrs_muex;
  1000. }
  1001. if (edev->max_supported) {
  1002. edev->extcon_dev_type.groups =
  1003. kzalloc(sizeof(struct attribute_group *) *
  1004. (edev->max_supported + 2), GFP_KERNEL);
  1005. if (!edev->extcon_dev_type.groups) {
  1006. ret = -ENOMEM;
  1007. goto err_alloc_groups;
  1008. }
  1009. edev->extcon_dev_type.name = dev_name(&edev->dev);
  1010. edev->extcon_dev_type.release = dummy_sysfs_dev_release;
  1011. for (index = 0; index < edev->max_supported; index++)
  1012. edev->extcon_dev_type.groups[index] =
  1013. &edev->cables[index].attr_g;
  1014. if (edev->mutually_exclusive)
  1015. edev->extcon_dev_type.groups[index] =
  1016. &edev->attr_g_muex;
  1017. edev->dev.type = &edev->extcon_dev_type;
  1018. }
  1019. ret = device_register(&edev->dev);
  1020. if (ret) {
  1021. put_device(&edev->dev);
  1022. goto err_dev;
  1023. }
  1024. #if defined(CONFIG_ANDROID)
  1025. if (switch_class)
  1026. ret = class_compat_create_link(switch_class, &edev->dev, NULL);
  1027. #endif /* CONFIG_ANDROID */
  1028. spin_lock_init(&edev->lock);
  1029. edev->nh = devm_kzalloc(&edev->dev,
  1030. sizeof(*edev->nh) * edev->max_supported, GFP_KERNEL);
  1031. if (!edev->nh) {
  1032. ret = -ENOMEM;
  1033. goto err_dev;
  1034. }
  1035. for (index = 0; index < edev->max_supported; index++)
  1036. RAW_INIT_NOTIFIER_HEAD(&edev->nh[index]);
  1037. dev_set_drvdata(&edev->dev, edev);
  1038. edev->state = 0;
  1039. mutex_lock(&extcon_dev_list_lock);
  1040. list_add(&edev->entry, &extcon_dev_list);
  1041. mutex_unlock(&extcon_dev_list_lock);
  1042. return 0;
  1043. err_dev:
  1044. if (edev->max_supported)
  1045. kfree(edev->extcon_dev_type.groups);
  1046. err_alloc_groups:
  1047. if (edev->max_supported && edev->mutually_exclusive) {
  1048. for (index = 0; edev->mutually_exclusive[index]; index++)
  1049. kfree(edev->d_attrs_muex[index].attr.name);
  1050. kfree(edev->d_attrs_muex);
  1051. kfree(edev->attrs_muex);
  1052. }
  1053. err_muex:
  1054. for (index = 0; index < edev->max_supported; index++)
  1055. kfree(edev->cables[index].attr_g.name);
  1056. err_alloc_cables:
  1057. if (edev->max_supported)
  1058. kfree(edev->cables);
  1059. err_sysfs_alloc:
  1060. return ret;
  1061. }
  1062. EXPORT_SYMBOL_GPL(extcon_dev_register);
  1063. /**
  1064. * extcon_dev_unregister() - Unregister the extcon device.
  1065. * @edev: the extcon device instance to be unregistered.
  1066. *
  1067. * Note that this does not call kfree(edev) because edev was not allocated
  1068. * by this class.
  1069. */
  1070. void extcon_dev_unregister(struct extcon_dev *edev)
  1071. {
  1072. int index;
  1073. if (!edev)
  1074. return;
  1075. mutex_lock(&extcon_dev_list_lock);
  1076. list_del(&edev->entry);
  1077. mutex_unlock(&extcon_dev_list_lock);
  1078. if (IS_ERR_OR_NULL(get_device(&edev->dev))) {
  1079. dev_err(&edev->dev, "Failed to unregister extcon_dev (%s)\n",
  1080. dev_name(&edev->dev));
  1081. return;
  1082. }
  1083. device_unregister(&edev->dev);
  1084. if (edev->mutually_exclusive && edev->max_supported) {
  1085. for (index = 0; edev->mutually_exclusive[index];
  1086. index++)
  1087. kfree(edev->d_attrs_muex[index].attr.name);
  1088. kfree(edev->d_attrs_muex);
  1089. kfree(edev->attrs_muex);
  1090. }
  1091. for (index = 0; index < edev->max_supported; index++)
  1092. kfree(edev->cables[index].attr_g.name);
  1093. if (edev->max_supported) {
  1094. kfree(edev->extcon_dev_type.groups);
  1095. kfree(edev->cables);
  1096. }
  1097. #if defined(CONFIG_ANDROID)
  1098. if (switch_class)
  1099. class_compat_remove_link(switch_class, &edev->dev, NULL);
  1100. #endif
  1101. put_device(&edev->dev);
  1102. }
  1103. EXPORT_SYMBOL_GPL(extcon_dev_unregister);
  1104. #ifdef CONFIG_OF
  1105. /*
  1106. * extcon_get_edev_by_phandle - Get the extcon device from devicetree
  1107. * @dev - instance to the given device
  1108. * @index - index into list of extcon_dev
  1109. *
  1110. * return the instance of extcon device
  1111. */
  1112. struct extcon_dev *extcon_get_edev_by_phandle(struct device *dev, int index)
  1113. {
  1114. struct device_node *node;
  1115. struct extcon_dev *edev;
  1116. if (!dev)
  1117. return ERR_PTR(-EINVAL);
  1118. if (!dev->of_node) {
  1119. dev_dbg(dev, "device does not have a device node entry\n");
  1120. return ERR_PTR(-EINVAL);
  1121. }
  1122. node = of_parse_phandle(dev->of_node, "extcon", index);
  1123. if (!node) {
  1124. dev_dbg(dev, "failed to get phandle in %s node\n",
  1125. dev->of_node->full_name);
  1126. return ERR_PTR(-ENODEV);
  1127. }
  1128. mutex_lock(&extcon_dev_list_lock);
  1129. list_for_each_entry(edev, &extcon_dev_list, entry) {
  1130. if (edev->dev.parent && edev->dev.parent->of_node == node) {
  1131. mutex_unlock(&extcon_dev_list_lock);
  1132. of_node_put(node);
  1133. return edev;
  1134. }
  1135. }
  1136. mutex_unlock(&extcon_dev_list_lock);
  1137. of_node_put(node);
  1138. return ERR_PTR(-EPROBE_DEFER);
  1139. }
  1140. #else
  1141. struct extcon_dev *extcon_get_edev_by_phandle(struct device *dev, int index)
  1142. {
  1143. return ERR_PTR(-ENOSYS);
  1144. }
  1145. #endif /* CONFIG_OF */
  1146. EXPORT_SYMBOL_GPL(extcon_get_edev_by_phandle);
  1147. /**
  1148. * extcon_get_edev_name() - Get the name of the extcon device.
  1149. * @edev: the extcon device
  1150. */
  1151. const char *extcon_get_edev_name(struct extcon_dev *edev)
  1152. {
  1153. return !edev ? NULL : edev->name;
  1154. }
  1155. static int __init extcon_class_init(void)
  1156. {
  1157. return create_extcon_class();
  1158. }
  1159. module_init(extcon_class_init);
  1160. static void __exit extcon_class_exit(void)
  1161. {
  1162. #if defined(CONFIG_ANDROID)
  1163. class_compat_unregister(switch_class);
  1164. #endif
  1165. class_destroy(extcon_class);
  1166. }
  1167. module_exit(extcon_class_exit);
  1168. MODULE_AUTHOR("Chanwoo Choi <cw00.choi@samsung.com>");
  1169. MODULE_AUTHOR("Mike Lockwood <lockwood@android.com>");
  1170. MODULE_AUTHOR("Donggeun Kim <dg77.kim@samsung.com>");
  1171. MODULE_AUTHOR("MyungJoo Ham <myungjoo.ham@samsung.com>");
  1172. MODULE_DESCRIPTION("External connector (extcon) class driver");
  1173. MODULE_LICENSE("GPL");