extcon.c 35 KB

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